Unable to Post values via RestAssured - api

Am newbee. Developing code using Restassured
Below is my code
JSONObject request = new JSONObject(); // created an object
request.put("Name", "this is a");
request.put("ID", "012");
given().
header("Content-Type","application/json").
body(request.toJSONString()).
when().
post("http://localhost:3000/Shop").
then().
statusCode(201).
log().ifError();
My JSON File:
{
"Shop":[
{
"ID": "001",
"Name": "ABC"
},
{
"ID": "002",
"Name": "XYZ"
}
]
}
Am getting exception message as
Expected status code <201> but was <500>.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
Can any one let me know what is wrong in my code?

As far as I understand, your created json is
{
"ID": "012",
"Name": "this is a"
}
but it requires
{
"Shop":[
{
"ID": "001",
"Name": "ABC"
},
{
"ID": "002",
"Name": "XYZ"
}
]
}
So you need to change the object of post body. My quick fix for this case is
Map<String, Object> item = new HashMap<>();
item.put("Name", "this is a");
item.put("ID", "012");
JSONObject shop = new JSONObject();
shop.put("shop", Collections.singleton(item));
given().log().all()
.contentType(ContentType.JSON)
.body(shop.toJSONString()).
.when()
.post("your-url")
.then()
.statusCode(201)
.log().ifError();

Related

Fetch data in a POST Request in asp.net core

I am using an external web link to get data and fetch it to json The reason why I need to handle it by the controller is to filter the data of it. Sadly, an api link was programmatically incorrect because instead of requesting it as GET method, it was programmed as POST method. I had this code simple code below but the return was a header data not the actual data of the api.
[HttpPost, Route("get/subproject")]
public ActionResult subproject()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(#"https://thisisjustasample.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage hrm = client.PostAsync("/api/new/get/subproject/details/get_dto", null).Result;
return Ok(hrm);
}
}
The output of the code above is this.
{
"version": "1.1",
"content": {
"headers": [
{
"key": "Content-Length",
"value": [
"29942142"
]
},
{
"key": "Content-Type",
"value": [
"application/json; charset=utf-8"
]
},
{
"key": "Expires",
"value": [
"-1"
]
}
]
}
}
What I need is this data below.
{
"sub_project_id": 267892,
"engineeringMigrationId": 0,
"modality_id": 21,
"id": null,
"reportID": null,
"month": null,
"year": null,
"cycle_id": 204
}
Any help would be appreciated. Thanks in advance.
Don't return hrm directly, If you want to get the response data, you need return.
hrm.Content.ReadAsStringAsync().Result
Demo
1.return Ok(hrm);
2.return Ok(hrm.Content.ReadAsStringAsync().Result);

In SendEmail MSGRAPH API InternalServerError,Object reference not set to an instance of an object

Tried to use Microsoft Graph Email API, BUT getting following error.
ODATA Payload
"message": {
"subject": "Send email",
"body": {
"contentType": "text",
"content": "wuirbmndf"
},
"toRecipients": [
{
"emailAddress": {
"address": "emailid"
}
}
],
"attachments": [
{
"#odata.type": "#microsoft.graph.fileAttachment",
"name": "attachment.txt",
"contentType": "text/plain",
"contentBytes": "SGVsbG8gV29ybGQh"
}
],
"from": {
"emailAddress": {
"address": "sender email id"
}
}
}
My Code Looks Like ->
client.BaseUrl = new Uri(String.Format("https://graph.microsoft.com/v1.0/me/sendmail"));
RestRequest request = new RestRequest();
request.Method = Method.POST;
request.AddParameter("Authorization", string.Format("Bearer " + emailObj.AccessToken), ParameterType.HttpHeader);
request.AddParameter("application/json", json, ParameterType.RequestBody);
//request.AddHeader("ContentType", "application/json");
//request.JsonSerializer.ContentType = "string";
var result = client.Execute(request);
And I am Getting InternalServerError with error Object reference not set to an instance of an object.
{
"error": {
"code": "InternalServerError",
"message": "Object reference not set to an instance of an object.",
"innerError": {
"request-id": "22ec4d88-2140-497f-bf28-80c83d7aa6a9",
"date": "2020-05-27T04:24:14"
}
}
}
thanks in advance.

Unable to create epic using postman

URL: http://localhost:8080/rest/api/2/issue
body:
{
"fields":
{
"project":
{
"key": "DEMO"
},
"Epic Name": "Epic Name 01",
"summary": "REST EXAMPLE1",
"description":"Creating an Epic via REST",
"issuetype":
{
"name": "Epic"
}
}
}
Jira will return 415 Unsupported Media Type if Content-type wasn't specified.
Add to your header:
Content-Type: application/json
I tried this and it worked:
{
"fields": {
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"customfield_10104": "Epic Name 01",
"issuetype": {
"name": "Epic"
},
"project":
{
"key": "JIRA"
}
}
}
If you'd like to create an issue with "Epic Name" you should refernce it using it's customfield ID.
To get "Epic Name" customfield id: use the following rest:
http://localhost:2990/jira/rest/api/2/customFields?search=Name%20of%20Epic

RestSharp How to display json on page

I have a call to a service that returns some json.
{
"channels": {
"22802056": {
"id": "22802056",
"title": "TV Live 1",
"url": "http://www.ustream.tv/channel/XBccccHefj",
"tiny_url": "http://ustre.am/1xss6",
"broadcast_urls": [
"rtmp://sjc-uhs-omega06.ustream.tv/ustreamVideo/22802ccc",
"rtmp://sjc-uhs-omega13.ustream.tv/ustreamVideo/22802ccc",
"rtmp://sjc-uhs-omega15.ustream.tv/ustreamVideo/22802ccc"
],
"status": "offline",
"description": "",
"owner": {
"id": "37134940",
"username": "xxxx_align",
"picture": "https://ustvstaticcdn1-a.akamaihd.net/images/defaults/user_48x48:3.png"
},
"authority": {
"reason": "own"
},
"default": true
},
"22802058": {
"id": "22802058",
"title": "TV Live 2",
"url": "http://www.ustream.tv/channel/DjAccczbPJ",
"tiny_url": "http://ustre.am/1ssR8",
"broadcast_urls": [
"rtmp://sjc-uhs-omega06.ustream.tv/ustreamVideo/228020ccc",
"rtmp://sjc-uhs-omega13.ustream.tv/ustreamVideo/228020ccc",
"rtmp://sjc-uhs-omega15.ustream.tv/ustreamVideo/228020ccc"
],
"status": "offline",
"description": "",
"owner": {
"id": "37134940",
"username": "xxxx_align",
"picture": "https://ustvstaticcdn1-a.akamaihd.net/images/defaults/user_48x48:3.png"
},
"authority": {
"reason": "own"
}
}
},
"paging": {
"actual": {
"href": "https://api.ustream.tv/users/self/channels.json?p=1"
}
}
}
This gets me the JSON:
IRestResponse jsonResponse = client.Execute(request);
In the example above there are only two channels displayed. In reality there are dozens of channels. How can I use this object and display all the channels and future channels on a page? If I paste this as a class in Visual Studio it creates a seperate class for each channel?
Any help is appreciated!
I had to use NewtonSoft json.net to get this to work for me.
string jsonResult = LoadJson();
dynamic objStreams = JObject.Parse(jsonResult);
var channelsData = ((JObject)objStreams.channels).Children();
foreach(JToken channelToken in channelsData)
{
var channeldeatils = channelToken.Children();
foreach (JToken properties in channeldeatils)
{
lstBox.Items.Add("ID : " + properties["id"].ToString());
lstBox.Items.Add("Title : " + properties["title"].ToString());
lstBox.Items.Add("URL : " + properties["url"].ToString());
lstBox.Items.Add("--------------------------------------------");
}
}

Google API post request to IBM blockchain error

I am trying to send reqest from google api script to IBM Blockchain SaaS project Bluemix.
The code is:
var url = "https://24f7d912-60f4-4eaf-89c6-b3e34b2247f9_vp1-api.blockchain.ibm.com:443/chaincode";
var headers =
{"jsonrpc": "2.0",
"method": "query",
"params": {
"type": 1,
"chaincodeID": {
"name": "c86de893fe7203e09add8c47237ffa377814311d9c3cb21ca4d3d5c4eeca65294d801a1f2e5a2d6b22b91096d9c8e42910046d2ab02d295ad0a0fb0b716e9a69"
},
"ctorMsg": {
"function": "query",
"args": ["b"]
},
"secureContext": "dashboarduser_type0_953add49d4"
},
"id": 1
};
var options =
{
"method" : "post",
"headers" : headers,
"MuteHttpExceptions":false
};
var response = UrlFetchApp.fetch(url,options);
var responseString=response.getContentText();
}
The error message is:
{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request","data":"Client must supply a payload for chaincode requests."},"id":null}
The request from Firefix Poster was answered correctly.
You have specified query as parameter to method and function but if you are trying to query using custom function ( Then specify method as query and function as custom function name that you have created in chaincode) as:
{"jsonrpc": "2.0",
"method": "query",
"params": {
"type": 1,
"chaincodeID": {
"name": "chainId"
},
"ctorMsg": {
"function": "customfunctionName",
"args": ["b"] //Argument to that custom function
},
"secureContext": "dashboarduser_type0_953add49d4"
},