Page 1 of 1

POST error with JSON data

Posted: Mon May 09, 2016 2:36 pm
by pink
I am trying to POST data to Backendless (which is a backend service in case you couldn't guess)
I am out of ideas as to how to troubleshoot this, can anyone notice something wrong or recommend anything else I can check?

so theURL is: https://api.backendless.com/v1/users/login
and theData is: {"login" : "greg@mad.pink", "password" : "REDACTED"}

and when I use this command: post theData to URL theURL

I get the following:
1. it and urlResponse both say "Expecting JSON data"
2. the result says "error 400 Bad Request"
3. and from libURLLastRHHeaders() I get:
HTTP/1.1 400 Bad Request
Access-Control-Allow-Headers: Origin, application-id, application-type, Content-Type, secret-key, request, user-token
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE, PATCH
Access-Control-Allow-Origin: *
Content-Type: text/plain; charset=utf-8
Date: Mon, 09 May 2016 13:20:48 GMT
Server: nginx/1.8.1
Content-Length: 19
Connection: keep-alive

The following curl call works the way it should:
curl -H application-id:XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX \
-H secret-key:XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX \
-H Content-Type:application/json \
-H application-type:REST \
-X POST \
-v 'https://api.backendless.com/v1/users/login' \
-d '{"login" : "greg@mad.pink", "password" : "REDACTED"}'

Re: POST error with JSON data

Posted: Mon May 09, 2016 3:16 pm
by LCNeil
Hi Greg,

This should give you what you need :)

Code: Select all

   set the httpHeaders to \
         "application-id: **APPIDHERE**" & cr & \
         "secret-key: **SECRETKEYHERE**" & cr & \
         "Content-Type: application/json"
   
   put ("{"&quote&"login"&quote&":"&quote&"test@livec.com"&quote&","&quote&"password"&quote&":"&quote&"lcisaweome"&quote&"}") into tPost
   
   post tPost to url "https://api.backendless.com/v1/users/login" 
Cheers,

Neil

Re: POST error with JSON data

Posted: Mon May 09, 2016 3:36 pm
by pink
I should've mentioned that I am setting the httpHeaders before the post attempt...

I did try running the command and still keep getting the "Expecting JSON Data" error

Code: Select all

command backend.headers pToken
     put "application-id: " & backendAppID & cr into tHeaders
     put "secret-key: " & backendRestID & cr after tHeaders
     if pToken is not empty then 
          put "user-token:" & pToken & cr after tHeaders
     end if
     put "Content-Type:application/json" & cr after tHeaders
     put "application-type: " after tHeaders
     set httpHeaders to tHeaders
end backend.headers

Re: POST error with JSON data

Posted: Mon May 09, 2016 3:52 pm
by LCNeil
Hi greg,

Did you try with my example?

I get the expected response from backendless
{"lastLogin":1462805460209,"created":1462802060000,"name":"ssss","___class":"Users","user-token":"******","ownerId":*******","updated":1462803281000,"email":"test@tes.com","objectId":"F3-0434-4C8A-FF03-E12128EB7F00","__meta":"{\"relationRemovalIds\":{},\"selectedProperties\":[\"__updated__meta\",\"password\",\"created\",\"name\",\"___class\",\"ownerId\",\"updated\",\"email\",\"objectId\"],\"relatedObjects\":{}}"}
cheers,

Neil

Re: POST error with JSON data

Posted: Tue May 10, 2016 3:17 pm
by pink
stupid me, I tried part of your code, but not all of it... I used my own headers, and therein seemed to be the problem

I was using "Content-Type:application/json"
you used: "Content-Type: application/json"
and that space made all the difference, which I find odd for three reasons:
1. the header in the cURL command did not use a space and it worked fine
2. the header worked fine for put commands, just not post commands
3. it's always worked fine without the space when interacting with other services

Thanks Neil, next time I promise not to half-ass test an example.

Re: POST error with JSON data

Posted: Tue May 10, 2016 3:23 pm
by LCNeil
Hmm that does seem like quite an annoying one. By habit, i've always put spaces after each header parameter but a space is an easy one to miss.

Glad you have it working now though :)

Cheers,

Neil

Re: POST error with JSON data

Posted: Tue May 10, 2016 3:33 pm
by pink
The frustrating part is that this is the third thing this year that I've posted on the forums that turned out to be one single missing character, the others. being a missing "s" and a missing carriage return.

Re: POST error with JSON data

Posted: Thu Jul 14, 2022 3:31 pm
by makeshyft
thank you both, this thread helped me alot 6 years later.