HTTP Post to an application/json content type

Started by theorem5, June 18, 2015, 03:03:34 PM

Previous topic - Next topic

theorem5

I am not entirely sure this can be done, or maybe I am doing something wrong.  For the record, I am no web expert, so forgive me if I say something incorrectly.  I am attempting to do a post to a site that is using a content type of application/json, and I keep getting an error 400, that tells me that I am making a bad request.  This is my code:

AddExtender("WWINT44I.DLL")
currdir=Dirget()
FileDelete("%currdir%tracefile.txt")
DebugTrace(@ON,"%currdir%tracefile.txt")

proxy = ""

bbsize=1000
bb=BinaryAlloc(bbsize)
   
BinaryEODSet(bb,bbsize)
bbaddr=IntControl(42,bb,0,0,0)
endofcontent=0 
endofcontent = iContentData(bbaddr, endofcontent,"", '{"loginId":"user1", "password":"password1"}' )
endofcontent = iContentData(bbaddr, endofcontent,"","")

;Setup proxy for connection as well as http or https
If proxy == ""
   tophandle=iBegin(0,"","")
else
   tophandle=iBegin(1,proxy,"")
endif

Debug(@ON)
   
connecthandle=iHostConnect(tophandle,"myserver",@HTTP,"","")
datahandle=iHttpInit(connecthandle,"POST","/myportal/api/login/authorization-tokens","",0)

myheader=StrCat("content-type: application/json")
   
rslt=iHttpOpen(datahandle,myheader,bbaddr,endofcontent)

outputfile=strcat("%currdir%","result.txt")
xx=iReadData(datahandle,outputfile)

BinaryFree(bb)
exit

                 
The error I get is:

{"timestamp":"2015-06-18T21:57:09.367+0000","status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read JSON: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n at [Source: java.io.PushbackInputStream@23a1833c; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n at [Source: java.io.PushbackInputStream@23a1833c; line: 1, column: 3]","path":"/myportal/api/login/authorization-tokens"}   

Am I doing this incorrectly?                                 

td

Purely a wild guess but perhaps encode the URL before you pass it to the 'iHttpInit' functions.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade