West Wind Internet and Client Tools
difficulties with HTTPGET POST with JSON
Gravatar is a globally recognized avatar based on your email address. difficulties with HTTPGET POST with JSON
  Terry Y
  All
  Nov 7, 2023 @ 10:55am

My VFP9 / wwHTTP code sends generated JSON and gets a 400 error (bad request) from server, yet when server techsupport uses my JSON with POSTMAN gets a 200 response. (wwclient.app is dated: 23.01.13) Here is my code:

base_url = "https://localhost:5000/v1/api/"
endpoint = "iserver/account/DU6877556/orders/whatif"
SENDTO = BASE_URL + ENDPOINT
loHTTP.AddPostKey()
lohttp.addheader()		&&	Clears data
loHttp.cContentType = "application/json"
loHttp.cHTTPVerb = "POST"
quantity = INT((CASH/20)/F_ENTRY)
IF quantity > 0
	CUSTOMORDER = ALLTRIM(SYMBOL) + "-" + SUBSTR(DTOC(DATE()), 1, 2) + SUBSTR(DTOC(DATE()), 4, 2) + SUBSTR(DTOC(DATE()), 7, 2) + "-STK"
	
	loSer = CREATEOBJECT("wwJsonSerializer")
	loMaster = CREATEOBJECT("EMPTY")
	loDetail = CREATEOBJECT("EMPTY")
	loDetails = CREATEOBJECT("COLLECTION")

	loSer.PROPERTY(loMaster,"orders", loDetails)

*	loSer.PROPERTY(loDetail,"coid",customorder)
	loSer.PROPERTY(loDetail,"conid",CONID)
	loSer.PROPERTY(loDetail,"orderType","LMT")
	loSer.PROPERTY(loDetail,"listingexchange","SMART")
	loSer.PROPERTY(loDetail,"price",round(F_ENTRY,2))
	loSer.PROPERTY(loDetail,"side","BUY")
	loSer.PROPERTY(loDetail,"tif","DAY")
	loSer.PROPERTY(loDetail,"quantity",QUANTITY)
	loSer.PROPERTY(loDetail,"outsideRTH", .F.)

	lodetails.ADD(loDetail)

	lcJSON = JsonSerialize(loMaster)
*	lcJSON = JsonSerialize(loMaster, .F.)
	
	? "ORDER sent to:"
	?  SENDTO
	? "JSON sent: "
	? lcJSON
	?
	OrderEntry = ""
	lohttp.post("https://localhost:5000/v1/api/tickle")
	
	loHTTP.AddPostKey(lcJson)
	? loHTTP.cHTTPheaders
	?
	? loHttp.GetHttpHeader("Content-Type")
	?
	
	order_01 = lohttp.httpget(SENDTO)
	? "order_01 results: ",order_01

Here is the results:

ORDER sent to:
https://localhost:5000/v1/api/iserver/account/DU6877556/orders/whatif
JSON sent: 
{"orders": [{"conid": 477176272,"listingexchange": "SMART","ordertype": "LMT","outsiderth": false,"price": 54.57,"quantity": 4,"side": "BUY","tif": "DAY"}]}

HTTP/1.1 200 OK
Referrer-Policy: Origin-when-cross-origin
x-response-time: 0ms
Content-Type: application/json;charset=utf-8
X-Content-Type-Options: nosniff
Expires: Sat, 04 Nov 2023 14:22:21 GMT
Cache-Control: max-age=0, no-cache, no-store
Pragma: no-cache
Date: Sat, 04 Nov 2023 14:22:21 GMT
Connection: keep-alive
Set-Cookie:
x-sess-uuid=0.17cbd517.1699107741.1cac33d1;
secure; HttpOnly
Server-Timing: cdn-cache; desc=MISS
Server-Timing: edge; dur=16
Server-Timing: origin; dur=64
Vary: origin,Accept-Encoding
Server-Timing: ak_p;
desc="1699107741483_399887127_481047505_7920_4498_
26_0_-";dur=1
Transfer-Encoding: chunked


application/json;charset=utf-8

order_01 results:  {"error":"Unknown order type"}

ORDER ENTRY Error:        400.00  -  Bad Request

400
Bad Request
Gravatar is a globally recognized avatar based on your email address. re: difficulties with HTTPGET POST with JSON
  Rick Strahl
  Terry Y
  Nov 7, 2023 @ 12:05pm

Don't know, but make sure your JSON properties have the correct casing. So for example, probably should be orderType not ordertype and therefore not found.

You need to use PropertyNameOverrides to fix up any proper field names that require anything other than lower case.

wwJsonSerializer::PropertyNameOverrides

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: difficulties with HTTPGET POST with JSON
  Terry Y
  Terry Y
  Nov 11, 2023 @ 05:57am

Thank you for your help in this matter. It worked!

Gravatar is a globally recognized avatar based on your email address. re: difficulties with HTTPGET POST with JSON
  Rick Strahl
  Terry Y
  Nov 11, 2023 @ 02:11pm

Where is the API reference?

I think they are not telling you what they are doing correctly. I am almost certain the problem is that the field names are not properly cased - because very few external APIs use all lower case for property key names.

Use Postman (or WebSurge) yourself with your JSON and see what you get and if it fails fix up the field names. Heck just look at the API docs and make sure the property names match and try it again and likely it'll work.

Look you're getting an error message and it specifically points at the orderType.

Also not sure what you're showing here, but this looks like a local request if it's going to localhost:5000. You're not getting a response from some other server, but something you're running on your local machine.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: difficulties with HTTPGET POST with JSON
  Terry Y
  Terry Y
  Nov 12, 2023 @ 02:37am

Agreed! The documentation is not the best I have ever seen. But, as you point out: "Make sure my variables match the documentation's letter-casing."

Thanks again for your insights. -Terry

© 1996-2024