I need help ...
When I use Postaman the 2 requests I make work correctly, but this code of mine only returns me the authentication correctly but when I create a new request, it does not return anything. The response_1 content returns this error: "{" error_code ":" platform.web_application "," correlation_id ":" d7em3oxz7ng6kh9qx977l3904 "," description ":" HTTP 404 Not Found "," description_translated ":" HTTP 404 Not Found ", "properties": null, "business_error": false} "
and the ResponseUri of response_1 is concatenated with that of the authentication. I can't find the error.This is the c# code
var client = new RestSharp.RestClient(url + #"/authentication/sign_in");
var request = new RestSharp.RestRequest(RestSharp.Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", request.AddJsonBody(new { user = admUserPass[0], password = admUserPass[1] }), RestSharp.ParameterType.RequestBody);
ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
RestSharp.IRestResponse response = client.Execute(request);
//response.StatusCode = "OK"
request.Resource = url + #"/admin/users";
request.Method = RestSharp.Method.GET;
foreach (var cookie in response.Cookies)
{
request.AddCookie(cookie.Name, cookie.Value);
}
request.AddHeader("content-type", "application/json");
request.AddHeader("Accept", "application/json");
RestSharp.IRestResponse response_1 = client.Execute(request);
Create a new Request before executing the second client.Execute(...)
OK I found the solution, my code is right but I am working to consume the Octane ALM API rest.
adding this line in the request header "request.AddHeader("ALM_OCTANE_TECH_PREVIEW "," true ");"
The request is working correctly. There is a discussion at this link https://community.microfocus.com/adtd/alm_octane/f/ngabetageneralbetaforum/161440/alm_octane_tech_preview-header-issue
Related
I have a rest request. The body is like the following:
RestClient client = new RestClient(uri);
RestRequest request = new RestRequest("", Method.POST);
request.AddHeader("Authorization", $"Bearer {myToken}");
request.AddHeader("Content-type", "application/json");
string json = "{'body': {'contentType': 'html','content': '" + message + "'}}";
request.AddJsonBody(json);
IRestResponse response = client.Execute(request);
The message contains apostrophes, like John's so the json will be
string json = "{'body': {'contentType': 'html','content': 'John's'}}";
This gives an error, Bad Request.
How can i accommodate for this and any other special characters in the message
you have to replace ' with \ "
string json= "{\"body\": {\"contentType\": \"html\",\"content\": \""+message+"\"}}";
I'm trying to use RestSharp to POST some data, however I'm encountering an error with my JSON content in the IRestResponse response.
Error:
"{"status":"error","message":"Invalid input JSON on line 1, column 96: Unexpected character ('e' (code 101)): was expecting comma to separate Object entries",...}"
From the message I assume I'm missing a comma at the end of the "content" line to separate the objects but can't figure out how to do it? Or do I need to place my name/value pairs into their own objects?
var client = new RestClient("https://api.com/APIKEY");
var request = new RestRequest(Method.POST);
request.AddHeader("accept", "application/json");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"properties\":{" +
"\"pipeline\":\"0\"," +
"\"content\":\"<b>Email: </b>\"" + user.Email + "\"<br/><b>Error: </b>\"" + string.Join("<br/>", type.Name.Split(',').ToList()) + "\"<br/><b>UserId: </b>\"" + user.userId + "\"<br/><b>RequestId: </b>\"" + callbackData.idReference +
"\"subject\":\"Error for\"" + user.FirstName + " " + user.LastName +
"\"owner_id\":\"001\"}}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
You should never manually build your own JSON! You should use an (anonymous) object and a dedicated JSON serialiser like Json.Net.
That being said RestSharp allows you to specify an object as the parameter value for AddParameter but you should probably be using AddJsonBody for the request body.
There are several mistakes in your manual JSON generation like missing quotes and commas at the end of the "content" and "subject" lines.
var client = new RestClient("https://api.com/APIKEY");
var request = new RestRequest(Method.POST);
request.AddHeader("accept", "application/json");
request.AddHeader("content-type", "application/json");
var parameter = new
{
properties = new {
pipeline = "0",
content = $"<b>Email: </b>\"{user.Email}\"<br/><b>Error: </b>\"{string.Join("<br/>", type.Name.Split(',').ToList())}\"<br/><b>UserId: </b>\"{user.userId}\"<br/><b>RequestId: </b>\"{callbackData.idReference}",
subject = $"Error for \"{user.FirstName} {user.LastName}\"",
owner_id = "001"
}
};
request.AddJsonBody(parameter)
IRestResponse response = client.Execute(request);
Doing it this way is much less error prone.
I want to send a request to a rest API and get response and show it on the form. the API documentation says that I should use my token in bearer to Authorization in the header.
I've used RestSharp nugget and this my written code:
var client = new RestClient("https://api.payping.ir");
var request = new RestRequest("v1/product/List");
request.AddHeader("Authorization", "Bearer <myToken>");
request.AddParameter("offset", 0);
request.AddParameter("limit", 10);
var response = client.Execute(request, Method.GET);
richTextBox2.Text = "Status:\n" + response.StatusCode + "\nContent:\n" + response.Content + "\nResponse:\n" + response.IsSuccessful;
but I receive an incorrect response. my final string that returns on my form by this code is:
Status:
0
Content:
Response:
False
what is my mistake? and how can I get a correct response from a rest API?
thank you for your attention
Try :
request.AddParameter("Authorization", $"Bearer {myToken}", ParameterType.HttpHeader);
Then inspect your sent packet using Fiddler to verify header is sent.
Next step is server-side then ;)
I'm trying to send a GET request with a token authentication, but i get an unauthorized response.
If i send the same request on Postman, it works.
Here's my code :
string url = string.Format("{0}batchs", MyUrl);
RestClient client = new RestClient(url);
RestRequest getRequest = new RestRequest(Method.GET);
getRequest.AddHeader("Accept", "application/json");
getRequest.AddHeader("Authorization", "token " + MyToken);
getRequest.AddParameter("name", MyName, ParameterType.QueryString);
IRestResponse getResponse = client.Execute(getRequest);
And here's my postman request :
Postman request
Any ideas on how to correct this ?
Thanks !
I'm not sure exactly what kind of auth you're using, but I use a firebase token generated at runtime, and this is the only thing I could get to work for me.
request.AddHeader("authorization", "Bearer " + _fireBaseService.AuthToken);
I can get an access token of Office 365. I can not make a REST request (GET) attaching this token in the header.
I'm using this code:
RestClient client = new RestClient();
client.EndPoint = #"https://outlook.office365.com/api/v1.0/me/folders/inbox/messages?$top=10";
client.Method = HttpVerb.GET;
client.ContentType = "application/json";
client.PostData = "authorization: Bearer " + myAccesToken.ToString();
String json = client.MakeRequest();
I've tested the access token in http://jwt.calebb.net and it's ok.
But it's always returning:
The remote server returned an error: (400) Bad Request.
I'm kind a knewby to REST and my english is not that good... Sorry! :)
(RE)EDIT
I've tried with RestSharp and I've simplified a bit my code...
Now I'm using my access token to make the GET request.
How do I add the "authorization bearer" to my request?
Is it like this?
//Ask for the token
var client = new RestClient("https://login.windows.net/common/oauth2/token");
var request = new RestRequest(Method.POST);
request.AddParameter("grant_type", "authorization_code");
request.AddParameter("code", Request.QueryString["code"]);
request.AddParameter("redirect_uri", myRedirectUri);
request.AddParameter("client_id", myClientID);
request.AddParameter("client_secret", myClientSecret);
IRestResponse response = client.Execute(request);
string content = "[" + response.Content + "]";
DataTable dadosToken = (DataTable)JsonConvert.DeserializeObject<DataTable>(content);
//I don't need a DataTable, but it was a way to retrieve my access token... :)
//Ask for info with the access token
var client2 = new RestClient("https://outlook.office365.com/api/v1.0/me");
var request2 = new RestRequest(Method.GET);
request2.AddHeader("authorization", myToken.ToString());
//I've tried this way also:
//client2.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(dadosToken.Rows[0]["access_token"].ToString(), "Bearer");
IRestResponse response2 = client2.Execute(request2);
string content2 = "[" + response2.Content + "]";
Response.Write(content2); //this returns NOTHING!
Thanks again!
You can also use Fiddler to figure out if the Request is well formed.
Try a simpler endpoint first like: https://outlook.office365.com/api/v1.0/me
and check if the right data comes back. You can call this endpoint just from the browser and also look at the request/respond inside Fiddler.
The first thing to check: Is it a bad request. This usually means the method can't be found or the given parameters cannot be located. Check the deploy and make sure it is the most up to date version and also check that your server is actually running.