1. Juan Manuel Jove Estigarribia
  2. PowerBuilder
  3. Monday, 24 February 2020 17:26 PM UTC

Hello all,

i am starting to develop a New PB app that must call a Python RestAPI
this is the code i have in Python working and need to translate it to PB
I must send and image as a parameter

import requests

url = "http://10.0.0.10:5005/subir/"

payload = {'token': 'mlk41kALGOKzdls',
'bucket': 'ordenes'}
files = [
('file', open('/path/to/file','rb'))
]
headers = {
'Content-Type': 'multipart/form-data'
}

response = requests.request("POST", url, headers=headers, data = payload, files = files)

print(response.text.encode('utf8'))

 

Thanks!

Armeen Mazda @Appeon Accepted Answer Pending Moderation
  1. Monday, 24 February 2020 21:10 PM UTC
  2. PowerBuilder
  3. # 1

Are you using PB 2019?  Are you using the new RESTClient object?

Comment
There are no comments made yet.
Juan Manuel Jove Estigarribia Accepted Answer Pending Moderation
  1. Tuesday, 25 February 2020 16:19 PM UTC
  2. PowerBuilder
  3. # 2
Yes, I am using PB 2019. I am using RestClient object.
Comment
There are no comments made yet.
Kevin Ridley Accepted Answer Pending Moderation
  1. Tuesday, 25 February 2020 17:01 PM UTC
  2. PowerBuilder
  3. # 3

You have to look at the API itself if you want to use something like the RESTClient, although based on your url looks like it's local on your machine or at least inside your own firewall probably.  Is the API published anywhere? 

 

One thing you can do is capture the HTTP request using a tool like Fiddler or Wireshark.  Capture the successful request and duplicate it using the HTTPClient object.  There's plenty of examples in the Appeon Community.  You can also take a look at my using DropBox REST API from PowerBuilder video with code attached.  There are many other samples as well if you search the site.  It's obviously not specific to your API, but you should get the general idea. 

https://www.appeon.com/developers/library/videos/using-dropbox-rest-api-powerbuilder.html

It doesn't look like it uses any oauth tokens, so you won't need the OAUTHClient.

Comment
  1. Juan Manuel Jove Estigarribia
  2. Tuesday, 25 February 2020 17:30 PM UTC
The token goes as a body parameters.

It works from PostMan with the following parameters:



Headers:

Key Values



Content-Type multipart/form-data



Body:

Key Values



token mlas11kALGOasda

bucket orders

file homero.jpg
  1. Helpful
There are no comments made yet.
Juan Manuel Jove Estigarribia Accepted Answer Pending Moderation
  1. Tuesday, 25 February 2020 17:36 PM UTC
  2. PowerBuilder
  3. # 4

It works from PostMan with the following parameters
Attachments (2)
Comment
  1. Kevin Ridley
  2. Tuesday, 25 February 2020 22:47 PM UTC
I gave you the info you need below. If you are looking for someone to code it for you, just look me up on LinkedIN and I can help you out, or one of the implementation partners can help as well. :)
  1. Helpful
  1. Juan Manuel Jove Estigarribia
  2. Tuesday, 25 February 2020 22:59 PM UTC
Hi Kevin!

Sorry for the inconvenience.

It was not my intention that someone codify my problem.

I could not make it work with the examples. Maybe it's a problem of the Api. Thank you very much really!
  1. Helpful
There are no comments made yet.
Mark Lee @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 26 February 2020 08:46 AM UTC
  2. PowerBuilder
  3. # 5

Hi Juan,

  1. Can you change the key "Content-Type" to value "application/json;charset=UTF-8" and using raw --> JSON as the body setting format then test it again in the Postman ( see attachment) ?  We need to confirm whether the Rest API provider supports JSON format or not.
  1. You can refer to the following links to see if they can resolve your problem.

https://docs.appeon.com/appeon_online_help/pb2019r2/application_techniques/ch18s02.html#d0e13932

https://docs.appeon.com/appeon_online_help/pb2019r2/powerscript_reference/ch10s678.html

https://docs.appeon.com/appeon_online_help/pb2019r2/powerscript_reference/ch10s680.html

 

Regards,

Attachments (1)
Comment
  1. Juan Manuel Jove Estigarribia
  2. Thursday, 27 February 2020 19:58 PM UTC
Thank You Mark!

Your links were very helpful for me!
  1. Helpful
There are no comments made yet.
mike S Accepted Answer Pending Moderation
  1. Wednesday, 26 February 2020 14:50 PM UTC
  2. PowerBuilder
  3. # 6

Juan,

post the PB script that you have so far, and from there we can perhaps see what you are missing.

Comment
  1. Juan Manuel Jove Estigarribia
  2. Thursday, 27 February 2020 19:57 PM UTC
Thank you Mike!



Finally this code worked!



Blob lblob_file,&

lblob_data

Integer li_FileNum,li_rtc

String ls_url = 'http://10.0.0.10:5005/subir',&

ls_uuid,&

ls_boundary,&

ls_token='mlk41kALGOKzdls',&

ls_bucket='ordenes'

httpclient lhc_httpclient



li_FileNum = FileOpen('c:\homero.jpg',StreamMode!)



IF li_FileNum = -1 THEN Return -1



li_rtc = FileReadEx(li_FileNum, lblob_file)



IF li_rtc = -1 THEN Return -2



FileClose(li_FileNum)



lhc_httpclient = create httpclient



ls_boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW'



lhc_httpclient.SetRequestHeader("Content-Type", "multipart/form-data; boundary="+"~r~n"+ls_boundary)



//Data

lblob_data = blob ("--" + ls_boundary + "~r~n" + &

'Content-Disposition: form-data; name="token"' + &

"~r~n~r~n" + ls_token + "~r~n", EncodingUTF8!)



lblob_data += blob ("--" + ls_boundary + "~r~n" + &

'Content-Disposition: form-data; name="bucket"' + &

"~r~n~r~n" + ls_bucket + "~r~n", EncodingUTF8!)



lblob_data += blob ("--" + ls_boundary + "~r~n" + &

'Content-Disposition: form-data; name="file"; filename="homero.jpg"' + &

"~r~n" + "Content-Type:image/jpeg" + "~r~n~r~n", EncodingUTF8!) +&

lblob_file + Blob("~r~n--"+ls_boundary+"--" + "~r~n", EncodingUTF8!)



lhc_httpclient.SetRequestHeader("Content-Length", String(len(lblob_data)))



li_rtc = lhc_httpclient.SendRequest( 'POST', ls_url, lblob_data)



IF li_rtc <> 1 THEN Return -3



li_rtc = lhc_httpclient.GetResponseStatusCode()



MessageBox("",li_rtc)



IF li_rtc <> 200 THEN Return li_rtc



lhc_httpclient.GetResponseBody(ls_uuid)



MessageBox("Uuid!",ls_uuid)



Return 1
  1. Helpful
There are no comments made yet.
Juan Manuel Jove Estigarribia Accepted Answer Pending Moderation
  1. Thursday, 27 February 2020 19:42 PM UTC
  2. PowerBuilder
  3. # 7
Finally this code worked!
Thank you Mike!

 

 

Blob lblob_file,&
lblob_data
Integer li_FileNum,li_rtc
String ls_url = 'http://10.0.0.10:5005/subir',&
ls_uuid,&
ls_boundary,&
ls_token='mlk41kALGOKzdls',&
ls_bucket='ordenes'
httpclient lhc_httpclient

li_FileNum = FileOpen('c:\homero.jpg',StreamMode!)

IF li_FileNum = -1 THEN Return -1

li_rtc = FileReadEx(li_FileNum, lblob_file)

IF li_rtc = -1 THEN Return -2

FileClose(li_FileNum)

lhc_httpclient = create httpclient

ls_boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW'

lhc_httpclient.SetRequestHeader("Content-Type", "multipart/form-data; boundary="+"~r~n"+ls_boundary)

//Data
lblob_data = blob ("--" + ls_boundary + "~r~n" + &
'Content-Disposition: form-data; name="token"' + &
"~r~n~r~n" + ls_token + "~r~n", EncodingUTF8!)

lblob_data += blob ("--" + ls_boundary + "~r~n" + &
'Content-Disposition: form-data; name="bucket"' + &
"~r~n~r~n" + ls_bucket + "~r~n", EncodingUTF8!)

lblob_data += blob ("--" + ls_boundary + "~r~n" + &
'Content-Disposition: form-data; name="file"; filename="homero.jpg"' + &
"~r~n" + "Content-Type:image/jpeg" + "~r~n~r~n", EncodingUTF8!) +&
lblob_file + Blob("~r~n--"+ls_boundary+"--" + "~r~n", EncodingUTF8!)

lhc_httpclient.SetRequestHeader("Content-Length", String(len(lblob_data)))

li_rtc = lhc_httpclient.SendRequest( 'POST', ls_url, lblob_data)

IF li_rtc <> 1 THEN Return -3

li_rtc = lhc_httpclient.GetResponseStatusCode()

MessageBox("",li_rtc)

IF li_rtc <> 200 THEN Return li_rtc

lhc_httpclient.GetResponseBody(ls_uuid)

MessageBox("Uuid!",ls_uuid)

Return 1

Comment
  1. Michael Kramer
  2. Friday, 28 February 2020 00:56 AM UTC
¡Hola, Juan!

THX for sharing. Great for future developers looking for working solutions!

Tip: I often create and use this constant for easier reading of string expressions: CONSTANT string EOL = "~r~n"

. . . + BLOB(EOL + "--" + ls_boundary + "--" + EOL, EncodingUTF8!)
  1. Helpful
  1. Juan Manuel Jove Estigarribia
  2. Friday, 28 February 2020 01:02 AM UTC
Great!!!

Thank you Michael!!
  1. Helpful
  1. Kevin Ridley
  2. Friday, 28 February 2020 13:30 PM UTC
Juan, that looks an awful lot like the script and video I gave you the link to in my first reply. Probably could've saved yourself some time if you had checked it out, but glad you got it working.



Cheers,

KR
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.