How to set header in Swift for http request

Hi, I tried to create a http request manually in Swift. Here is my code:

    let url = URL(string: "http://localhost:1337/parse/test")!
    let requestUrl = url    // Prepare URL Request Object
    var request = URLRequest(url: requestUrl)
    
    request.setValue("application-idValue", forHTTPHeaderField: "X-Parse-Application-Id")
    request.setValue("secret-keyValue", forHTTPHeaderField: "b10bb5d0587e4518a20031605636ab85")
    
    request.httpMethod = "POST"

But, Xcode says “Connection refused”. Is my request header setting correct?

Thanks

Hi, I changed

    request.setValue("application-idValue", forHTTPHeaderField: "X-Parse-Application-Id")
    request.setValue("secret-keyValue", forHTTPHeaderField: "b10bb5d0587e4518a20031605636ab85")

request.setValue("b10bb5d0587e4518a20031605636ab85", forHTTPHeaderField: "X-Parse-Application-Id").

It works because it can bring the json data back from the server. However, it also generate some errors:

**SwiftUI_practice[95348:133487072] [connection] nw_socket_handle_socket_event [C2.1:2] Socket SO_ERROR [61: Connection refused]**
**Response data string:**
**{"tokenA":123455}**

Any idea why? Thanks

The easiest way to use Swift with Parse is using the Swift SDK as it uses REST in the necessary ways to connect to a Parse Server.

If you insist on writing your own API (this will not be easy) then you can look for guidance in:

@cbaker6 Thanks. It is not exactly what I asked for. But it is definitely useful to check the documents.

When I am fetching data without using the SDK, that is via the REST services I set these header values:
request.addValue(“application/json”, forHTTPHeaderField: “Content-Type”)
request.addValue(myRestValue, forHTTPHeaderField: “X-Parse-REST-API-Key”)
request.addValue(myApplicationID, forHTTPHeaderField: “X-Parse-Application-Id”)

Hope this helps.

Halldor