How do I send this multipart/form-data to a Web Service using Genexus? - genexus

The Following CUrl works perfectly whenever I test it:
curl --request PUT \
--url https://api.sandbox.URL_HERE/document-analysis/90471702021 \
--header 'Accept: application/json' \
--header 'Authorization: Bearer AUTH_TOKEN' \
--header 'Content-Type: multipart/form-data; boundary=---011000010111000001101001' \
--header 'api-version: 1.0' \
--form documentType=RG \
--form documentSide=FRONT \
--form 'image=data:image/jpeg;name=RG_Front.jpg;base64,/9j/4AAQSkZJRgABAQEAAQABAAD/4Tn0RXhpZgAATU0AKgAAAAgABgALAAIAAAAmAAAIYgESAAMAAAABAAEAAAExAAIAAAAmAAAIiAEyAAIAAAAUAAAIrodpAAQAAAABAAAIwuocAAcAAAgMAAAAVgAAEUYc6gAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA8JeLF8wysb6N95+837vqfyr2KvwCwP8VH2ABmkb5RQGx2qNm8zhTj3rk5rH0xImduKSY7UP0NPHFMlXcpHTrWiE9j8eNcuLbT/FWpXcipPi/uC1uyNIANzjHHBIyDjOOKyLW8eOS7gNrCTdRF7O3RiESOLUbZY4IdzF41TcQC2R87bm5bmv0mK4xg4HpSKvvUuTvoP6nT7n5yXf7KPxc1ZZVu/Blg6SzNM6HWwgJBCoC6PvYJGCqqWIGScE1auP2SPi/N5/k6PpzPNGgaS81OKRvMRsrIw2FXO0BeRkjqa/RPb6mk9s5p8zBYOmj88ZP2Q/i2bGZY9G0a3U5aOFdV3m2YjD+QzIfJDDIKgkYPGOt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xTc84xUlZWTO0jf/V5709elFFa9QFooopgMXvT6KKACiiigAooooAKKKKACkHSiikAtFFFCAKKKKYBRRRQAVG1FFSwFX7tPoopIBrULRRR1AdTKKKUtgCnUUUgEXvUUP8AqxRRUMCSiiihbgFFFFNgMqWiinABaKKK2AKKKKACiiigAooooAZJ3qP+9RRWD3Akp9FFVED/2Q=='
The Base64 string above was cut just to fit the comment field, but you get the idea.
I just have to send some info to the endpoint using HTTPClient but got stuck especially because Genexus says that the line is too big when I try to convert the image (1Mb) to a base64 string.
Has anyone gone through that?

It seems that the Web Service is expecting the new "multipart/form-data" instead of the old "application/x-www-form-urlencoded" for uploading files as explained here and the specification section for forms in HTML 4.01 is here. So, going to your question, I think that you can use the AddFile method of the HTTPClient data type (as explained here) to get your client working. Readink the wiki, it says:
In particular, if the content of the HttpClient message is multipart / form-data (given by Content-Type Header), then the AddFile method adds the file as multipart. In this case, the second parameter is needed to indicate the name of the variable that the file represents. Also, you have to necessarily precede your sentence with a header that specifies a specific Content-Type, like this:
&Httpclient.Addheader("Content-Type", !"multipart/form-data")
I say "I think" because I dindn't test myself, you can try and let us know if it works or not.

Related

postman binary data-type detect and save in laravel request

In postman there is an option to send binary datatype in a api.
I am sending that in a api where but i am unable to detect that binary file in laravel request method.
curl --location --request POST 'http://192.168.*.*/api/v1/contact_us/save' \
--header 'Authorization: Bearer some_key' \
--header 'Content-Type: image/jpeg' \
--data-binary '#/Users/username/Desktop/filename.jpeg'
Above is my curl code generated in postman.
Although i am successfully getting this file when i request in multipart/form-data
I did see a question related to it but it is not the exact case
related question
If the HTTP request payload is purely a file (ie. not from a HTML form) then you can access it in Laravel using:
$request->getContent()

Google People API - get contact details (batch)

Using Google People API, one can get a contact detail by its ID.
"https://people.googleapis.com/v1/" + id + "?key=" + <KEY> + "&personFields=addresses,clientData,emailAddresses,names,organizations,phoneNumbers,birthdays",
Is it possible to get such details in batch? I have a list of 100 contact ids and I want them to get at once as it'd be much faster.
In your situation, how about using the method of "Method: people.getBatchGet"? When this method is used, the 200 resource names (maximum number) can be retrieved by one API call. When this is used for your situation, it becomes as follows.
Sample curl command:
curl \
'https://people.googleapis.com/v1/people:batchGet?personFields=addresses%2CclientData%2CemailAddresses%2Cnames%2Corganizations%2CphoneNumbers%2Cbirthdays&resourceNames=people%2Fsample1&resourceNames=people%2Fsample2&resourceNames=people%2Fsample3' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed
In above case, the unencoded endpoint is https://people.googleapis.com/v1/people:batchGet?personFields=addresses,clientData,emailAddresses,names,organizations,phoneNumbers,birthdays&resourceNames=people/sample1&resourceNames=people/sample2&resourceNames=people/sample3.
Also, you can test this with "Try this API". Ref
Reference:
Method: people.getBatchGet

Soap wsa:To specification at curl command line

How can I compose a curl command line that contains a wsa:To Soap specification for example http://myserver.services.com/Services/ServiceUsers.svc?wsdl?
Well, in the end this is not what I wanted but I managed to achieve my goal. The solution was to use the --data argument followed by the xml request corresponding to the request and in it put the wsa:To header. By example:
curl --location --request POST 'http://myserver.services.com/Services/ServiceUsers.svc' --header 'Content-Type: application/soap+xml; charset=UTF-8' --data '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/"><soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>http://tempuri.org/IServiceUsers/UsersList</wsa:Action><wsa:To>http://myserver.services.com/Services/ServiceUsers.svc</wsa:To></soap:Header><soap:Body><tem:UsersList/></soap:Body></soap:Envelope>'
Thanks to #DingPeng for his help, you may be right about the availability of the .Net framework

How to POST a Laravel form with cURL from CLI?

I have a laravel application with a form, where upon a GET request (example.my.lan/form) of the formular the user receives a *_session cookie and a XSRF-TOKEN cookie. Now I'm trying to call the controller (example.my.lan/form/confirmation) via POST on the command line with cURL:
curl -vvv -k -X POST -d "param1=value1&param2=value2" \
-H "Content-Type: application/x-www-form-urlencoded" \
--cookie "my_form_session=a...z" \
--cookie "XSRF-TOKEN=a...z" https://example.my.lan/form/confirmation
curl -vvv -k -X POST -d "param1=value1&param2=value2" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "my_form_session=a...z" \
-H "XSRF-TOKEN=a...z" https://example.my.lan/form/confirmation
curl -vvv -k -X POST -d "param1=value1&param2=value2" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "my_form_session: a...z" \
-H "XSRF-TOKEN: a...z" https://example.my.lan/form/confirmation
From my browser everything works as expected. But if I call the controller on the CLI using cURL, the laravel app is always responding with a 419 (The page has expired). I know this is some cookie related issue, but still can't figure ou how to solve it - maybe someone has an idea?
So roughly speaking here's what needs to happen:
You need to perform an initial request to get a valid session cookie (which is essentially an encrypted session id) and along with that you also need to somehow obtain a valid CSRF token.
To get the token you have two options.
Visit a page which has a form which includes it via #csrf
Grab the cookie called XSRF-TOKEN which contains the encrypted csrf token
When you send the request you need to be sure you send the correct session cookie e.g. --cookie "my_form_session=a...z".
If you got the token from the #csrf field then you either send the header X-CSRF-TOKEN or as an additional form field _token=csrftoken
If you got the token from the XSRF-TOKEN cookie, which contains an encrypted CSRF token, this needs to go in the X-XSRF-TOKEN field.
By convention, all non-standard HTTP headers should be prefixed with X- (indicating an extension to the protocol) which is why those headers start with X-

GraphiQL - upload file

How can we upload files using in-browser GraphQL IDE GraphiQL, is that even possible ? (apart from base64 encoded string)
Once I have the file stream / file contents I can create a mulipart request and store on DB or some object-storage service.
But I am not able to figure it out how to provide the file input / how the schema would look like. Is it better to use graphql-upload with Curl request. Please suggest which is the optimal solution.
I was able to upload a file in my mutation input using Altair. You can use the Add files button to upload a file, then use the upload name on your mutation.
Guys just try Altair, saved me a lot of trouble and time. It has extension for chrome too, just like Apollo playground or graphiql, but with file upload option underneath the variables option.
Currently GraphIQL does not support file uploads. You can use an API tool to do this such as Postman, Insomnia or plan old cURL. The important distinction is that it needs to be a multi-part form upload.
Example request:
curl --request POST \
--url http://localhost:4000/ \
--header 'accept: application/json' \
--header 'accept-encoding: gzip, deflate, br' \
--header 'connection: keep-alive' \
--header 'dnt: 1' \
--header 'origin: http://localhost:4000' \
--form 'operations={"query": "mutation UploadFile($file: Upload!) { uploadFile(file: $file)}", "variables": { "file": null } }' \
--form 'map={ "nFile": ["variables.file"] }' \
--form nFile=#/tmp/testfile
Substitute /tmp/testfile in the request above with a path to the file you want to upload.

Resources