By
Mr_Microphone
Alright, I may be an idiot.
Three years ago, I wrote a program that pushed component information to a secure site via their API. I went back to add some attributes and (here's the idiot part) ended up losing the source code and my modified code does not quite work. I have the compiled version that works minus the new attributes, so I know that their system has not changed. I stripped the larger program down from 3,000 lines to the part that is broken, but I am stumped. This was one of my first scripts, so it heavily leverages examples and isn't as pretty as I'd like it to be.
Be gentle.
The program / script creates a new records as expected, but for some reason, I cannot access information in the response, which I need for a later step.
I use Charles, a web debugging proxy tool so I can see the request and the response and both are as expected. Also, when I write to log file, the JSON reply is exactly what I expect and need, but when I try to do anything with the http body, it seems to be blank.
Here is the script minus the URL and token:
#include <Array.au3>
#include <Curl.au3>
#include <MsgBoxConstants.au3>
#include <json.au3> ; this was added as an alternate way to read the data
Global $WM_serial_number = "WM20745001"
Global $wm_component_status_id = "10"
Global $wm_manufacturer ="Multi-Tech"
Global $wm_model = "MTR-LAT1-B07"
Global $cellular_carrier_id = "3"
Global $iccid_esn = "89010303300012345678"
Global $ip_address = "192.168.2.11"
Global $NewIDNumber
Local $Curl = Curl_Easy_Init()
Local $Html = $Curl ; any number as identify
Local $Header = $Curl + 1 ; any number as identify
Local $HtmlFile = "cURL_Request.html"
Local $File = FileOpen($HtmlFile, 2 + 16)
Local $Slist = Curl_Slist_Append(0, "content-type: multipart/form-data; boundary=---011000010111000001101001")
$Slist = Curl_Slist_Append($Slist, "authorization: Token token=" & $Token)
Curl_Easy_Setopt($Curl, $CURLOPT_PROXY, "127.0.0.1") ; needed to use Charles web debugging proxy
Curl_Easy_Setopt($Curl, $CURLOPT_PROXYPORT, 8888) ; needed to use Charles
Curl_Easy_Setopt($Curl, $CURLOPT_HTTPHEADER, $Slist) ;
Curl_Easy_Setopt($Curl, $CURLOPT_URL, $Server & "wireless_module" & "s")
Curl_Easy_Setopt($Curl, $CURLOPT_SSL_VERIFYPEER, 0)
Curl_Easy_Setopt($Curl, $CURLOPT_TIMEOUT, 30)
Curl_Easy_Setopt($Curl, $CURLOPT_WRITEDATA, $Html)
Curl_Easy_Setopt($Curl, $CURLOPT_WRITEFUNCTION, Curl_FileWriteCallback())
Curl_Easy_Setopt($Curl, $CURLOPT_WRITEDATA, $File)
Local $HttpPost = ""
Local $LastItem = ""
Curl_FormAdd($HttpPost, $LastItem, $CURLFORM_COPYNAME, "wireless_module" & "[serial_number]", $CURLFORM_COPYCONTENTS, $WM_serial_number, $CURLFORM_END)
Curl_FormAdd($HttpPost, $LastItem, $CURLFORM_COPYNAME, "wireless_module" & "[component_status_id]", $CURLFORM_COPYCONTENTS, $wm_component_status_id, $CURLFORM_END)
Curl_FormAdd($HttpPost, $LastItem, $CURLFORM_COPYNAME, "wireless_module" & "[manufacturer]", $CURLFORM_COPYCONTENTS, $wm_manufacturer, $CURLFORM_END)
Curl_FormAdd($HttpPost, $LastItem, $CURLFORM_COPYNAME, "wireless_module" & "[model]", $CURLFORM_COPYCONTENTS, $wm_model, $CURLFORM_END)
Curl_FormAdd($HttpPost, $LastItem, $CURLFORM_COPYNAME, "wireless_module" & "[cellular_carrier_id]", $CURLFORM_COPYCONTENTS, $cellular_carrier_id, $CURLFORM_END)
Curl_FormAdd($HttpPost, $LastItem, $CURLFORM_COPYNAME, "wireless_module" & "[iccid_esn]", $CURLFORM_COPYCONTENTS, $iccid_esn, $CURLFORM_END)
Curl_FormAdd($HttpPost, $LastItem, $CURLFORM_COPYNAME, "wireless_module" & "[ip_address]", $CURLFORM_COPYCONTENTS, $ip_address, $CURLFORM_END)
; submit
Curl_Easy_Setopt($Curl, $CURLOPT_HTTPPOST, $HttpPost)
Local $Code = Curl_Easy_Perform($Curl)
If $Code = $CURLE_OK Then
ConsoleWrite("Content Type: " & Curl_Easy_GetInfo($Curl, $CURLINFO_CONTENT_TYPE) & @LF)
ConsoleWrite("Download Size: " & Curl_Easy_GetInfo($Curl, $CURLINFO_SIZE_DOWNLOAD) & @LF)
MsgBox(0, 'Html', BinaryToString(Curl_Data_Get($Html))) ; this is something I threw in for debugging, expecting to see SOMETHING. Returns nothing
MsgBox(0, 'Header', BinaryToString(Curl_Data_Get($Header))) ; this is something I threw in for debugging, expecting to see SOMETHING. Returns nothing
Local $response = Curl_Easy_GetInfo($Curl, $CURLINFO_RESPONSE_CODE)
If $response = "409" Then $response = "Failed due to a conflict."
If $response = "200" Then $response = "Was NOT created."
If $response = "201" Then $response = "Was created."
; read the ID that was assigned and store it
$NewIDNumber = StringRight(StringLeft(BinaryToString(Curl_Data_Get($Html)), 10), 4) ; this DID work, but now it doesn't. An old compiled version still works
;~ Global $JsonObject = json_decode($Html); another debugging attempt. Did not use json functions previously and the program worked without it.
;~ Global $NewIDNumber = json_get($JsonObject, '.id')
ConsoleWrite(@CRLF &'! id:' & $NewIDNumber & @CRLF & @CRLF) ; debugging feedback
MsgBox(0, $response, $wm_serial_number & " new ID = " & $NewIDNumber); debugging feedback
If $Code <> $CURLE_OK Then ConsoleWrite(Curl_Easy_StrError($Code) & @LF)
Local $Data = BinaryToString(Curl_Data_Get($Curl))
Curl_Easy_Cleanup($Curl)
Curl_Data_Cleanup($Curl)
Curl_Data_Cleanup($Header)
Curl_Data_Cleanup($Html)
Curl_FormFree($HttpPost)
Curl_slist_free_all($Slist)
curl_easy_reset($Curl)
FileClose($File)
ConsoleWrite(@LF)
EndIf
This is the captured request (minus the host and token)
POST /api/v2/wireless_modules HTTP/1.1
Host: api.
Accept: */*
authorization: Token token=
Content-Length: 942
Expect: 100-continue
content-type: multipart/form-data; boundary=---011000010111000001101001; boundary=------------------------9adb0d87c7ea5061
--------------------------9adb0d87c7ea5061
Content-Disposition: form-data; name="wireless_module[serial_number]"
WM20745001
--------------------------9adb0d87c7ea5061
Content-Disposition: form-data; name="wireless_module[component_status_id]"
10
--------------------------9adb0d87c7ea5061
Content-Disposition: form-data; name="wireless_module[manufacturer]"
Multi-Tech
--------------------------9adb0d87c7ea5061
Content-Disposition: form-data; name="wireless_module[model]"
MTR-LAT1-B07
--------------------------9adb0d87c7ea5061
Content-Disposition: form-data; name="wireless_module[cellular_carrier_id]"
3
--------------------------9adb0d87c7ea5061
Content-Disposition: form-data; name="wireless_module[iccid_esn]"
89010303300012345678
--------------------------9adb0d87c7ea5061
Content-Disposition: form-data; name="wireless_module[ip_address]"
192.168.2.11
--------------------------9adb0d87c7ea5061--
and the captured response
HTTP/1.1 201 Created
Date: Sun, 04 Apr 2021 00:12:18 GMT
Server: Apache
Cache-Control: max-age=0, private, must-revalidate
Access-Control-Allow-Origin: not-allowed
Vary: Accept-Encoding
Access-Control-Max-Age: 1728000
X-XSS-Protection: 1; mode=block
X-Request-Id: 71cfcf36-6020-48a6-a822-d2b393a27b69
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: PUT, OPTIONS, GET, POST
ETag: W/"25d97fe8a9387cb4b9029a9e62b0bfa2"
X-Frame-Options: SAMEORIGIN, SAMEORIGIN
X-Runtime: 0.344005
X-Content-Type-Options: nosniff
Access-Control-Request-Method: *
X-Powered-By: Phusion Passenger 5.2.1
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
Location: /wireless_modules/3195
Status: 201 Created
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
X-Charles-Received-Continue: HTTP/1.1 100 Continue
{"id":3195,"model":"MTR-LAT1-B07","serial_number":"WM20745001","manufacturer":"Multi-Tech","mfg_date":null,"iccid_esn":"89010303300012345678","ip_address":"192.168.2.11","purchase_order":null,"supplier":null,"cellular_carrier_id":3,"component_status_id":10,"component_status":{"id":10,"name":"Hold","description":"Available- Held for specific use"},"custom_attributes":[{"name":"Deactivated","type":"Boolean","value":false},{"name":"Port 3001","type":"Boolean","value":false}],"comments":[]}
Also attached is the log file. I need to read the id value. Clearly, it is arriving back to cURL, since it is being written out to the log, but I cannot seem to get to it within the code.
It is established that I may be an idiot, but this idiot has wasted days in non-billable hours trying to figure out what should be a simple glitch.
Help???
cURL_Request.html