I have been using RESTSHARP automating REST API test cases. So far I am familiar in using JObject like:
public IRestResponse HELPERS_POST(JObject input)
{
...
request.RequestFormat = DataFormat.Json;
request.AddParameter("text/json", input, ParameterType.RequestBody);
...
My problem now is I have a new API with the following information:
Parameters: nodeIds Parameter
Type: body
Data Type: Array[long] or as example {[12345,23453,45332]}
I parsed the above as JArray.
Now I don't know what to do with this JArray so my HELPERS_POST() can use it to make a POST request.
Related
I need to post a request from JSON file in my selenium test. The below code is showing response code as 400.
Manually tested, it's working fine and response code is 200 for the same json body.
When this json body is stored in a json file and executed the below code, then response code displays as 400.
String fileName = "D:/json/test1.json";
String json = FileUtils.readFileToString(new File(fileName), "utf-8");
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); // required url is already saved in 'url' variable.
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/JSON");
con.setRequestProperty("Accept", "application/JSON");
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
byte[] response = json.getBytes("utf-8");
out.write(response.toString());
System.out.println(httpConn.getResponseCode());
Please, appreciate any help on this?
Folks,
A total newbie here when it comes to making end-to-end integrations. I am trying to "put" my salesforce data to the s3 bucket but receiving:
The XML you provided was not well-formed or did not validate against our published schema
Here's what I am doing:
List<Task> tasks = new List<Task>([SELECT ID from TASK WHERE Id =:recordId LIMIT 1]);
for(Task task:tasks)
{
try
{
//File Content
String Body = JSON.serialize(task);
HttpRequest req = new HttpRequest();
req.setMethod('PUT');
req.setEndpoint('callout:AWS_Credentials');
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
req.setBody(Body);
Http http = new Http();
HTTPResponse res = http.send(req);
What might I be doing wrong here? Too lost to see..
Thanks in advance!
Set the content type as 'application/xml' instead of 'application/json;charset=UTF-8'
req.setHeader('Content-Type', 'application/xml');
It was really painful to find how to call Phishtank API here.
After a lot of searching I was able to find how to call the API. Below is a sample call,
https://checkurl.phishtank.com/checkurl/index.php?url=http://auto.smtpsystems.net/&format=json
But the problem with the above call is that it gives the response in XML format whereas I want the response in JSON format.
Any kind of help will be greatly appreciated.
The problem is that you are making an HTTP GET request. And this method accepts an HTTP POST request
//Custom your request
var requestOptions = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
url: "https://checkurl.phishtank.com/checkurl/",
method: 'POST',
json: true,
body: {
url: The URL to check(urlencoded or base64 encoded),
format: 'json',
app_key: Your application key
},
};
//Do the request
request.post(requestOptions, function callback(err, httpResponse, json) {
//Here you json
})
Make sure to use https instead of http in the endpoint url, although in Documentation http is given(as of writing this).
Use HTTP POST request not HTTP GET.
And format is in quotes(double preferred)
# Python implementation
endpoint = "https://checkurl.phishtank.com/checkurl/"
url = "http://www.travelswitchfly.com/"
response = requests.post(endpoint, data={"url": url, "format": "json"})
You have to specify the url, format, and the app_key in the body of the POST request.
I was trying to implement their API in my android application with the help of Retrofit. Their documentation is outdated. After spending 3 hours I come to know a few things.
use this URL https://checkurl.phishtank.com/checkurl/ (do not use URL with http://)
use the below interface for retrofit GET request. it does not work with #Query and it requires #FormUrlEncoded
#FormUrlEncoded
#GET("https://checkurl.phishtank.com/checkurl/")
fun findPhishing(
#Field("format") format: String,
#Field("url") url: String
): Single<Response>
I'm trying to create a REST API from a SOAP Service using IBM API Connect 5. I have followed all the steps described in this guide (https://www.ibm.com/support/knowledgecenter/en/SSFS6T/com.ibm.apic.apionprem.doc/tutorial_apionprem_expose_SOAP.html).
So, after dragging the web service block from palette, ensuring the correctness of endpoint and publishing the API, I have tried to call the API from the browser. Unfortunately, the API return the following message:
<errorResponse>
<httpCode>500</httpCode>
<httpMessage>Internal Server Error</httpMessage>
<moreInformation>Error attempting to read the urlopen response
data</moreInformation>
</errorResponse>
To testing purpose, I have logged the request and I have tried the request on SOAPUI. The service return the response correctly.
What is the problem?
In my case, the problem was in the backend charset (Content-Type: text/xml;charset=iso-8859-1).
For example, backend returns text/xml in German (or French). Api Connect cannot process character ΓΌ. It needs Content-Type: text/xml;charset=UTF-8.
I had a similar issue, in my case was the accept. if you have an Invoke and the content-type or the accept, is not matching the one of the request, or the response that you got, APIC is getting mad.
Please, check if the formats to send (contentType) and receive (accept) are the same of that your API expected. In my case the error occurs because the API returns a String and my default code is configured to receive a JSON body.
//define a JSON-PLAIN TEXT protocol
private HttpEntity<String> httpEntityWithBody(Object objToParse){
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + "xxx token xxx");
headers.set("Accept", MediaType.TEXT_PLAIN_VALUE);
headers.setContentType(MediaType.APPLICATION_JSON);
Gson gson = new GsonBuilder().create();
String json = gson.toJson(objToParse);
HttpEntity<String> httpEntity = new HttpEntity<String>(json, headers);
return httpEntity;
}
//calling the API to APIC...
ParameterizedTypeReference<String> responseType = new
ParameterizedTypeReference<String>(){};
ResponseEntity<String> result =
rest.exchange(builder.buildAndExpand(urlParams).toUri(), HttpMethod.PUT, httpEntityWithBody(myDTO), responseType);
String statusCode = result.getStatusCodeValue();
String message = result.getBody();
I am new in elm and try to consume web api using http post request with header and body using 0.17.1 version but did not get any documentation.
So any one help me to implement this functionality
The send method of the Http package gives you the possibility to create and send a custom request. For example, a post request could be something like
postRequest : Request
postRequest =
{ verb = "POST"
, headers =
[ ("Origin", "http://elm-lang.org")
, ("Access-Control-Request-Method", "POST")
, ("Access-Control-Request-Headers", "X-Custom-Header")
]
, url = "http://example.com/hats"
, body = empty
}
You can then create the Task that represent the request using the send function like
send defaultSettings postRequest