How do I convert a curl statement to a Java process? - java

I have this curl statement and I'm trying to convert it to something I can use in a java program for testing purposes:
curl -v -k -X POST -H 'Content-Type: application/json' -d '{"auth_data": {"email_password": {"email": "email#email.com", "password": "password"}},"registration_data": { "domain":"Device", "device_name": "name" ,"app_name": "XXX", "app_version": "XXX","device_model": "XXX","os_version": "XXX","device_type": "ABCDEFGHIJ","device_serial": "D01234567890"}, "requested_token_type" : ["bearer"]}' 'https://url.domain.com';

Either you call an external executable from your code like this question shows
Or you can use the libcurl java implementation

Related

To accept -d #json , How put method should be written in rest api

In PUT rest call , Using curl I would like to use -d option , what should be method signature . I am using java.
curl -X PUT -u testUser 'http://hostname:port/method' -d #test.json -H "Content-Type: application/json"
This should be my curl when I fire .
I have written a method signature which accepts string as input
#PUT #Path ("/test")
#Produces("application/json")
public Response method(String input) throws Exception {...}
When I run above command , Having above method signature is throwing error .
Error processing command: java.lang.NullPointerException
String input needs data in json format .
{
"name" : "test",
"host" : "10.xx.xx.xx",
"port" : 22
.
.
} //around 15 values
having these in a single url like this
"http://hostname:port/method?name=test&host=10.xx.xx.xx&port=22........."
makes url grow pretty long but its working though.
I would like to use -d option to send json input through a file.
What should be the method signature for curl to work with -d #test.json in java ?
--Thanks in Advance.

wiremock how to get post data

i'm trying to get data from the post does any one try
{
"name":"sasha",
"age":"15",
"country":"italy"
}
I will like to get the value sash from the post, any ideas?
only find to reply...
http://wiremock.org/docs/request-matching/
buy I don't want to reply, I want the data.
I would like something linke this:
String name = wiremock.withRequestBody(name());
print name -> sasha
something like this:
Body postBody = aResponse().withBody(matching("[?(#.name]"));
I'm trying to get data from the POST like curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d "{'json':{'name':'sasha'}}" 127.0.0.1:8080/to/post I want to get sasha

Convert Python request with image to cURL

I found this example of a API request. Unfortunately I didn't find any other example how to upload an image to the API.
As I'm not familiar with Python I'm trying to understand how to do the same in a cURL command.
import requests
auth_headers = {
'app_id': 'your_app_id',
'app_key': 'your_app_key'
}
url = 'https://XXXXXXX'
files = {
'source': open('media/test.jpg')
}
data = {
'timeout': 60
}
response = requests.post(url, files=files, data=data, headers=auth_headers)
I tried to convert it by trying out a cURL to python converter, but I don't know how to build it with the files.
In the end I want to do the request in JAVA, but I think if I would know the request in cURL I can figure it out.
Hope anyone can help me with that.
This will do it:
#!/bin/bash
args=(
-H 'app_id: your_app_id'
-H 'app_key: your_app_key'
-F 'source=#/path/to/file'
-F 'timeout=60'
'http://httpbin.org/post'
)
curl "${args[#]}"
or, as a one-liner:
curl -H 'app_id: your_app_id' -H 'app_key: your_app_key' -F 'source=#/path/to/file' -F 'timeout=60' 'http://httpbin.org/post'
Use -H to specify header fields (repeat for every field) and -F to specify form fields - either as key=value pairs, or filename=#path pairs. When -F is used, POST method is the default, and Content-Type is multipart/form-data (but that too can be overridden).

How to create campaign through Apple Search Ads API

I'm trying to create Campaign through Apple Search Ads API. So I use
curl -X POST https://api.searchads.apple.com/api/v1/campaigns \
--cert path/XXXX.p12 \
--pass **** \
-H "Authorization: orgId=xxxx" \
-d '{"budgetAmount":{"currency":"USD","amount": 50.0},"name": "weixinCampagin", "adamId":"414478124","adGroups": [{"name": "weixinCampaginAdGroup","startTime":"2017-03-17 00:00:00","defaultCpcBid": {"amount": 1,"currency":"USD"},"storeFronts": ["US"]}]}'
I got the following error messages:
{"data":null,"pagination":null,"error":{"errors":[{"messageCode":"INVALID_ATTRIBUTE_TYPE","message":"This is an invalid request. At least one field format is not readable by the system.","field":"Line#:1 Column#:50"}]}}
I have tried many times through different ways, but still not working. Is there any smart guys can help me?
Thanks in advance!
I think you need to specify that you're sending a json object.
curl \
--cert ./<FILENAME>.p12 \
--pass <PASSWORD> \
-H "Authorization: orgId=<ORG_ID>" \
-H "Content-Type: application/json" \
-d "<CAMPAIGN_DATA_FILE>.json" \
-X POST "https://api.searchads.apple.com/api/v1/campaigns"
Apart from content-type header,
The amount needs to be in string format
'{"budgetAmount":{"currency":"USD","amount": "50.0"},"name": "weixinCampagin", "adamId":"414478124","adGroups": [{"name": "weixinCampaginAdGroup","startTime":"2017-03-17 00:00:00","defaultCpcBid": {"amount": "1","currency":"USD"},"storeFronts": ["US"]}]}'
in https://developer.apple.com/library/content/documentation/General/Conceptual/AppStoreSearchAdsAPIReference/API_Overview.html
the sample also uses string for the amount item

Converting a Authenticated curl request to Apache Camel Request

curl -s -S -u user123:321pass https://12.15.13.12:3216 --data
'<?xml version="1.0" encoding="UTF-8"?><data>Hello Man</data>' -H 'Content-Type: text/xml' -k
I need to change this to a Apache Camel route call like :-
private ProducerTemplate producer;// there are setters for this.
producer.requestBodyAndHeaders(endpointUri, requestBody,addHeaders, String.class);
Main question is how do I pass username and password with this requestBodyAndHeaders.I tried passing through headers(header being map, key value pairs)

Categories

Resources