Convert REST curl command to Apex Request - rest

I have a curl example of a REST API call as follows
curl https://api.endpoint.com/api_action.json \
-u key:secret \
-d 'message=Hello World' \
-d id=12345
Now I'm trying to replicate this using the Apex HttpRequest class but I'm unsure as to how I should be passing the options into the call.
What I have so far is as follows
HttpRequest req = new HttpRequest();
req.setEndpoint(https://api.endpoint.com/api_action.json);
req.setMethod('POST');
// what goes in these
req.setHeader(stuff);
req.setBody(stuff);
Http http = new Http();
HttpResponse res = http.send(req);

From #sfdcfox answer
Given this comment:
curl -i -X POST --data "first_name=test&last_name=testlast&email=test#test.com&phone=1234567&company=Ac‌​meInc&years_in_business=3&amount=4000&dealer_id=524b2833f7494317db000001" https://example.com/webform/start/
The code you need would be approximately:
String payload='first_name=test&last_name=testlast&email=test#test.com&phone=1234567&company=Ac‌​meInc&years_in_business=3&amount=4000&dealer_id=524b2833f7494317db000001';
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://example.com/webform/start/');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setHeader('Content-Length',String.valueOf(payload.length()));
req.setBody(payload);
Http binding = new Http();
HttpResponse res = binding.send(req);

When you are using APEX, you can use the build-in apex_web_service.make_rest_request to do this:
declare
v_clob clob;
begin
v_clob := apex_web_service.make_rest_request(
p_url => <your URL>,
p_http_method => 'POST',
p_body => <whatever body>,
p_username => 'username',
p_password => 'password' );
htp.p(v_clob);
end;

Related

Use groovy-wslite make a post request from CURL request

Hi i have the following CURL request that i'd like to include in a groovy script using the groovy-wslite library put struggling to get the request working.
curl -s -X POST -k -u user:password https://artifactory_url/api/search/aql -H 'Content-Type: text/plain' -d 'items.find({"type":"file","repo":{"$eq": "my-repo-name"},"path":{"$match":"com/mycompany/product1/subcat/mob/*"},"name":{"$match":"*apk"}}).sort({"$desc":["path"]}).limit(1)'
You can use http-builder-ng and your code could look like
compile 'io.github.http-builder-ng:http-builder-ng-CLIENT:1.0.4'
HttpBuilder.configure {
request.uri = 'https://artifactory_url/api/search/aql'
request.auth.basic 'un', 'pw'
request.contentType = 'text/plain'
}.post {
request.body = 'items.find({"type":"file","repo":{"$eq": "my-repo-name"},"path":{"$match":"com/mycompany/product1/subcat/mob/*"},"name":{"$match":"*apk"}}).sort({"$desc":["path"]}).limit(1)'
response.success { FromServer fs, Object body ->
println body
}
}

MultiPart PUT request not working in spring boot

I have the following curl request
curl -X PUT http://localhost:50005/did:corda:tcn:77ccbf5e-4ddd-4092-b813-ac06084a3eb0 -H 'content-type:multipart/form-data' -F 'instruction=hgfhhf'
I am trying to read the instruction in my spring boot controller as seen below
#PutMapping(value = "{id}",
produces = arrayOf(MediaType.APPLICATION_JSON_VALUE),
consumes = arrayOf(MediaType.MULTIPART_FORM_DATA_VALUE))
fun createID(#PathVariable(value = "id") id: String,
#RequestParam("instruction") instruction: String ) : ResponseEntity<Any?>
But the code above returns
"status":400,"error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"Required request part 'instruction' is not present"
remove unused:
consumes = arrayOf(MediaType.MULTIPART_FORM_DATA_VALUE)
you have missed request param instruction (in reqeust), try this:
curl -X PUT -G 'http://localhost:50005/did:corda:tcn:77ccbf5e-4ddd-4092-b813-ac06084a3eb0' -d 'instruction=hgfhhf'
also take a look at CURL Command Line URL Parameters

How do I upload an image to Parse Server using Kotlin/Jvm via Rest Service?

I'm creating a Kotlin/Jvm (without Android Sdk) application that interacts with a instance of a Parse Server (Back4App). Unfortunately, parse doesn't provide a Sdk implementation to use with Java/Kotlin without Android.
So I'm using the rest Api. Now I trying to upload a image from my disk into Back4App file server. In the doc there is snippet using curl. But I wasn't able to translate into a Retrofit service:
curl -X POST \
-H "X-Parse-Application-Id: 4MGgDJ0ZiQloXoSTE2I9VM6YUYIz8EwCKF4pK7zr" \
-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
-H "Content-Type: image/jpeg" \
--data-binary '#myPicture.jpg' \
https://YOUR.PARSE-SERVER.HERE/parse/files/pic.jpg
So I based my implementation in this article and other snippets from GitHub and created a retrofit service for it:
#Multipart
#POST("/parse/files")
fun upload(
#Part file: MultipartBody.Part
): Call<ResponseBody>
And call:
var file = File("assets/escudo.png")
var requestFile = RequestBody.create(MediaType.parse("**/image"), file)
var body = MultipartBody.Part.createFormData("picture", file.name, requestFile)
var r = getService().upload(body).execute()
I created the retrofit instance as below:
fun getService(): ParserService {
val retrofit = Retrofit
.Builder()
.baseUrl("https://parseapi.back4app.com")
.addConverterFactory(GsonConverterFactory.create())
.client(createClient()).build()
return retrofit.create(ParserService::class.java)
}
fun createClient(): OkHttpClient {
return OkHttpClient.Builder().addInterceptor(createHeadInterceptor()).build()
}
fun createHeadInterceptor(): Interceptor {
return HeaderInterceptor()
}
class HeaderInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response =
chain.run {
val credentials = CredentialsUtils.readCredentials()
log.info { credentials }
proceed(
request().newBuilder()
// .addHeader("Content-Type", "application/json")
.addHeader("Content-Type", "image/png")
.addHeader("X-Parse-Application-Id", credentials.back4appAppId)
.addHeader("X-Parse-REST-API-Key", credentials.back4appRestApiKey)
.build()
)
}
}
I was able to use it to posting Json data (by uncommenting the content/type header). But when I tried to upload an image I receive this response:
Response{protocol=h2, code=400, message=, url=https://parseapi.back4app.com/parse/files}
More info:
-- EDIT
I tried a different approuch without Retrofit, it gives a 201 response code and gives me an objectId, but it doesn't upload the file:
val file2 = File("assets/escudo.png")
val serverUrl = "https://parseapi.back4app.com/classes/myfiles"
val url = URL(serverUrl)
val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "POST"
conn.doOutput = true
val postData = file2.readBytes()
conn.addRequestProperty("Content-length", postData.size.toString())
conn.setRequestProperty("Content-Type", "image/*")
conn.setRequestProperty("X-Parse-Application-Id", credentials.back4appAppId)
conn.setRequestProperty("X-Parse-REST-API-Key", credentials.back4appRestApiKey)
val outputStream = DataOutputStream(conn.outputStream)
outputStream.write(postData)
outputStream.flush()
println(conn.responseCode)
-- EDIT
Trying now using Khttp:
val file = File("assets/foto.jpg")
val file2 = File("assets/escudo.png")
val serverUrl = "https://parseapi.back4app.com/classes/myfiles"
val files = listOf(FileLike("foto.jpg", file), FileLike("escudo.png", file2))
val response = post(serverUrl, headers = getHeaders(), files = files)
println(response)
println(response.text)
}
fun getHeaders(): Map<String, String> {
return mapOf(
"Content-Type" to "image/*",
"X-Parse-Application-Id" to credentials.back4appAppId,
"X-Parse-REST-API-Key" to credentials.back4appRestApiKey
)
}
Getting this error:
<Response [400]>
{"error":"Unexpected token - in JSON at position 0"}
If you're using Back4App, the correct Server URL is:
https://parseapi.back4app.com/files/pic.jpg

Translating curl into Matlab/Webwrite

I have the following curl command I need to sent to a web server using Matlab and webwrite using POST. My problem is that I always get a "Bad request" answer so my syntax must be wrong somehow. Does anybody have an idea how this curl command, sending the body could look like in Matlab using webwrite in a correct way ?
body=$(cat << EOF
{
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
EOF
)
curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <AUTHENTICATION TOKEN>" \
-d "$body" \
"https://api-fxtrade.oanda.com/v3/accounts/<ACCOUNT>/orders"
I have just asked a potentially similar question so this may not work first time. However I cannot test without knowing some login details so I can but hope this helps.
data_InputValues = struct ('units',100,'instrument','EUR_USD','timeInForce','FOK','type','MARKET','positionFill','DEFAULT');
MyBody = matlab.net.http.MessageBody(struct('order',data_InputValues));
MyHTTPOptions = matlab.net.http.HTTPOptions(); % use this to change the options if necessary (e.g. extend timeout)
Request = matlab.net.http.RequestMessage;
Request.Method = 'POST';
Request.Header = matlab.net.http.HeaderField('Content-Type','application/json','Authorization: Bearer',AUTHENTICATION TOKEN);
Request.Body = MyBody;
uri = matlab.net.URI('https://api-fxtrade.oanda.com/v3/accounts/<ACCOUNT>/orders');
[response a ~] = Request.send(uri,MyHTTPOptions);
The part I struggle with is generating the MyBody part (in your case this is parsing the order variable's sub-variables). If you get this to work I would be keen to know how! P.S. my question in case it helps: Matlab RESTful PUT Command - net.http - nesting body values
The correct format for the body is as follows:
body = struct('units',100,'instrument','EUR_USD','timeInForce','FOK',...
'type','MARKET','positionFill','DEFAULT');
As for the HTTP headers that you require you can specify them with weboptions when using webwrite.
The syntax for an additional header:
options = weboptions('KeyName','Name','KeyValue','Value')
Where Name and Value are the name of the header and its value respectively.
You must add the headers that you require in weboptions.
For the code you provided, the correct syntax would be as follows:
options = weboptions('MediaType','application/json',...
'KeyName','Authorization: Bearer','KeyValue','Token');
You can then perform the POST request at the URL of interest.
response = webwrite(url,body,options);

How to get a paypal access token using REST request

I'm trying to get this working https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/
Using Java + Jersey application. It seems like I'm missing something in POST params.
public String getPaypalToken() {
Client client = Client.create();
WebResource webResource = client.resource("https://api.sandbox.paypal.com/v1/oauth2/token");
MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
queryParams.add("username", CLIENT_ID + ":" + SECRET );
queryParams.add("grant_type", "client_credentials");
ClientResponse response = webResource.accept("application/json").acceptLanguage("en_US").type("application/x-www-form-urlencoded").post(ClientResponse.class, queryParams);
return response.toString();
}
Using previous code I got: POST https://api.sandbox.paypal.com/v1/oauth2/token returned a response status of 401 Unauthorized.
This CURL command line option just works fine:
curl -X POST https://api.sandbox.paypal.com/v1/oauth2/token -H "Accept: application/json" -H "Accept-Language: en_US" -u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" -d "grant_type=client_credentials"
Any suggestions will be appreciated.
J.
The -u option in curl sends a base64 encoded "username:password" string. I don't think adding client id / secret to the queryParams map does the same (unless Jersey treats the 'username' key differently which I don't think it does).
You should instead try
webResource.header("Authorization", "Basic " + Base64.encode(CLIENT_ID + ":" + SECRET.getBytes()))
Just sharing my solution:
Dictionary<string, string> sdkConfig = new Dictionary<string, string>();
sdkConfig.Add("mode", "sandbox");
string clientid = "<your client id>";
string secretid = "<your secret id>";
string accessToken = new OAuthTokenCredential(clientid, secretid, sdkConfig).GetAccessToken();
I previously encountered unauthorized response using RestSharp, then found this. I'm using paypal .net sdk from nuget package.
Reference.