POST error with JSON data

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
pink
Posts: 272
Joined: Wed Mar 12, 2014 6:18 pm

POST error with JSON data

Post by pink » Mon May 09, 2016 2:36 pm

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"}'
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: POST error with JSON data

Post by LCNeil » Mon May 09, 2016 3:16 pm

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

pink
Posts: 272
Joined: Wed Mar 12, 2014 6:18 pm

Re: POST error with JSON data

Post by pink » Mon May 09, 2016 3:36 pm

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
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: POST error with JSON data

Post by LCNeil » Mon May 09, 2016 3:52 pm

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

pink
Posts: 272
Joined: Wed Mar 12, 2014 6:18 pm

Re: POST error with JSON data

Post by pink » Tue May 10, 2016 3:17 pm

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.
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: POST error with JSON data

Post by LCNeil » Tue May 10, 2016 3:23 pm

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

pink
Posts: 272
Joined: Wed Mar 12, 2014 6:18 pm

Re: POST error with JSON data

Post by pink » Tue May 10, 2016 3:33 pm

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.
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

makeshyft
Posts: 220
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Re: POST error with JSON data

Post by makeshyft » Thu Jul 14, 2022 3:31 pm

thank you both, this thread helped me alot 6 years later.
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”