sean7512 / RestEssentials

Lightweight REST and JSON library for Swift.
MIT License
37 stars 12 forks source link

Error performing POST: Error Domain=NSCocoaErrorDomain Code=3840 #21

Closed Etio39 closed 5 years ago

Etio39 commented 5 years ago

Hi Sean, First of all, thanks for your work.

I'm trying to make a POST request but I'm struggling ..

Using Postman, this is what I need to request : capture d ecran 2018-08-09 a 13 35 31

So this is the code I'm using, based on your example :

let postData2: JSON = ["emailAddress": "emailAddress", "password": "password"]

rest.post(postData2, at: "user") { result, httpResponse  in
        do {
            let json = try result.value()
            print(json)
       } catch {
            print("Error performing POST: \(error)")
       }
}

And when I'm trying to execute I'm getting this error :

Error performing POST: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start 
with array or object and option to allow fragments not set." 
UserInfo={NSDebugDescription=JSON text did not start with array or object and option 
to allow fragments not set.}

I hope that you can help me to understand my mistake.

Thanks in advance, Etienne.

sean7512 commented 5 years ago

@Etio39 It looks like the web server may not be returning json. Do you know what the server returns?

Also, from your screenshot, your login endpoint does NOT accept json, it looks like you're doing form encoded where the value of user is json.

Etio39 commented 5 years ago

@sean7512 Thanks for your repply.

Here is what the server returns :

{
    "status": "success",
    "user": {
        "emailAddress": "emailAdress",
        "password": "password",
        "id": 27,
        "token": "token",
        "firstName": "firstName",
        "lastName": "lastName",
        "gender": "Mr",
        "birthDate": ""
    }
}

So yes, it is JSON.

I thinks, as you said, that the problem comes from the login endpoint.

Thanks for your help.

sean7512 commented 5 years ago

@Etio39 let me know if you need this issue opened. I believe the issue is the login endpoint not accepting json.

If you can't change the endpoint, then you may want to look into another library, as RestEssentials does not currently support sending data as form-encoded.

Etio39 commented 5 years ago

@sean7512 since the error is from my side, yes you can close the issue. Thanks for helping me.

I'll check if the endpoint can be changed as I really like to work with your library !

Thank you.

sean7512 commented 5 years ago

@Etio39 Awesome, good luck and thank you!