I am trying to upload a PDF file to an API endpoint and Im getting this InvalidArgumentException. Can anyone help me figure out what Ive got wrong here?
Self.ClearRequestHeaders
var pdfstream As TextInputStream
var pdfstring as string
Try
pdfstream = TextInputStream.Open(pdffile)
pdfstream.Encoding = Encodings.MacRoman
pdfstring = pdfstream.ReadAll
Catch e As IOException
MessageBox("Error accessing file.")
End Try
pdfstream.Close
Self.RequestHeader("Content-Type") = "multipart/form-data"
Self.RequestHeader("Authorization") = "Bearer "+ myAPI.apitoken
var payload as string ="https://myAPI.com/api/v1/quickAdd?quoteID=" + id +_
"&itemID=null&machine=BigMachine&material=pick%material&thicknes=.125&thickness_Units=inch&qty=100&units=inch"
Self.SetRequestContent(pdfstring, "multipart/form-data")
Self.Send("POST", payload)
I see ClearRequestHeaders, are you re-using a socket? Try creating a new URLConnection for subsequent requests. Re-using a socket can lead to inexplicable problems so myself and some other people I respect recommend against doing so.
but I dont see any examples of how to format the file attachment. The API “try it out” just has a button to attach the file to the call at the bottom of the list of values in those key/val pairs. I am not getting the call to go out at all, its erroring in xojo.
Does the API accept another type of format? You have to construct multipart/form-data yourself in Xojo. I don’t have anything handy, but I know something exists… somewhere…
URLConnection does some validation before it makes an outbound request. Sometimes if the framework knows that it can’t form a valid request you get an exception.
Wow Andrew, thank you! I did run accross this in a post but I was thnking it must be part of something more complicated than I want to do but I am begining to see it is either this approach or Christian’s cURL classes.
Thanks Greg. I did catch that % should have been %20 but encoding all the values seems to have it working, Im not sure what the culprit was but those values will be fed by some other ui so I appreciate the solution. @Andrew_Lambert I did also use your old code and it was very minor to change - thank you immensly for that! I think all that had to be done was to change the httpsocket to a URLConnection.