authorization header does not have the right format - c#

Akamai api using purge my url i got eror he authorization header does not have the right format
getting error response httpresponse any one tell how to add header authorization in c# tell how to solve the issue ,,tell me how to call akamai api in our project .i have accesstoken and cilent token secret key also how to add this token in this header section
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http;
using System.Web.Script.Serialization;
using System.Net.Http.Headers;
public partial class Purge : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
purge();
}
public async static void purge()
{
var baseAddress = new Uri(" https://akab-wbrwrbgi6t5urrvg-ohjpi4v6gxsib5aa.purge.akamaiapis.net/");
using (var httpClient = new HttpClient { BaseAddress = baseAddress })
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("akab-jbl3s3ptctwvocrr-3fawkhddo47udqlg");
// httpClient.DefaultRequestHeaders.ExpectContinue = false;
using (var content = new StringContent("{ \"objects\": [ \"http://hindi.eenaduindia.com/News/National/2016/03/15150007/video-viral-of-a-girl-creating-ruckus-in-hyderabad.vpf\" ], \"action\": \"remove\", \"type\": \"arl\", \"domain\": \"production\"}", System.Text.Encoding.Default, "application/json"))
{
using (var response = await httpClient.PostAsync("ccu/v2/queues/default", content))
{
string responseData = await response.Content.ReadAsStringAsync();
var b = responseData.Replace('"', ' ');
var r = b.Split(',', '\n');
// Response.Write(r[1]);
}
}
}
}
}

You need to use an Akamai signing library. There is a library for C# here:
https://github.com/akamai-open/AkamaiOPEN-edgegrid-C-Sharp
The library's README includes sample code to get started integrating it into your system.
Thanks,
Kirsten

Related

automate the test case to validate the response “US” when the value for the input parameter “CurrencyCode” is “USD” Using C# RestSharp

I have developed a API using RapidAPI to get and automate test cases to validate response “US” when the value for the input parameter “CurrencyCode” is “USD” Using C# RestSharp.but it is fowing an error when its opening on .net console as below on get and IRestresponse.I need help to fix it.
here is my code.
using RestSharp;
using System;
using RestSharp.Authenticators;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IFSAPI
{
internal class Program
{
static void Main(string[] args)
{
//arrange
var client = new RestClient("https://wft-geo-db.p.rapidapi.com/v1/geo/countries?currencyCode=LKR");
var request = new RestRequest(Method.GET);
//
request.AddHeader("X-RapidAPI-Key", "2ade609583msh867a2c95e9e419dp1a1668jsnf1f880a47cc5");
request.AddHeader("X-RapidAPI-Host", "wft-geo-db.p.rapidapi.com");
//act
IRestResponse response = client.Execute(request);
//var content = response.Content;
//Console.WriteLine(content);
//Console.ReadKey();
}
}
}

POST function in RestSharp

I want to POST a file to an API by RestSharp, but the Method.Post encounter error as cannot convert from 'RestSharp.Method' to 'string?', and the error for the Method.POST is 'Method' does not contain a definition for 'POST'?
using RestSharp;
using System;
using System.Net;
using System.Net.Http;
namespace UploadToAzure
{
class Program
{
static void Main()
{
var client = new RestClient("http://localhost:7071/api/Function1");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddFile("File", "/D:/sample Files/audio0001.mp3");
IRestResponse response = (IRestResponse)client.Execute(request);
Console.WriteLine(response.Content);
}
}
}
Thanks for your answers!
It solved when I add string of destination to RestRequest and change IRestResponse to RestResponse. In addition correct the path of file.
using RestSharp;
using System;
using System.Net;
using System.Net.Http;
namespace UploadToAzure
{
class Program
{
static void Main()
{
var client = new RestClient("http://localhost:7071/api/Function1");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddFile("File", #"D:/sample Files/audio0001.mp3");
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
}
}
I had similar issue, but solved it by doing the following two things
Changed POST to Post.
Putting Method.Post in double quotes "", i.e treating it as a string.

required page redirect based on success response on asp

I am new to development currently working on one development task on asp and c#, from the website when ever user filled the form and click submit it will trigger the webhook post method on backend and i am capturing the response also but the problem is its displaying directly on the webpage instead i want redirect to different webpage and say custom message based on the response its like when ever i get success i want to redirect page and say your request hasbeen successfully submitted else your request hasbeen failed
below is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http;
using Newtonsoft.Json;
using System.Text;
namespace azuremfareset
{
public partial class index : System.Web.UI.Page
{
private static readonly HttpClient client = new HttpClient();
protected void Page_Load(object sender, EventArgs e)
{
}
protected async void btnsubmit_Click(object sender, EventArgs e)
{
try
{
lblmsg.Text = "submitting....";
if (!string.IsNullOrEmpty(txtemail.Text))
{
var values = new Dictionary<string, string>
{
{ "emailid", txtemail.Text}
};
var jsonObject = JsonConvert.SerializeObject(values);
var content = new StringContent(jsonObject, Encoding.UTF8, "application/json");
string url = System.Configuration.ConfigurationManager.AppSettings["webhook_url"];
var response = await client.PostAsync(url, content);
lblmsg.Text = await response.Content.ReadAsStringAsync();
}
else
{
lblmsg.Text = "enter emailid";
}
}
catch(Exception ex)
{
lblmsg.Text = ex.Message;
}
}
}
}
You can redirect to a page using Response.Redirect(page.aspx);
This redirects a request to a new URL and specifies the new URL.
You will need to add the namespace System.Web. Example: using System.Web
You can find the Microsoft Documentation on it by clicking this link

.net core HTTPS requests returns 502 bad gateway while Postman returns 200 OK

What's wrong with this code snippet in C#.NET core 3:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
var uriBuilder = new UriBuilder
{
Scheme = Uri.UriSchemeHttps,
Host = "api.omniexplorer.info",
Path = "v1/transaction/address",
};
var req = new Dictionary<string, string>
{
{ "addr", "1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA" }
};
using(var httpClient = new HttpClient())
{
var response = await httpClient.PostAsync(uriBuilder.Uri, new StringContent(JsonConvert.SerializeObject(req)));
response.EnsureSuccessStatusCode();
Console.WriteLine(response.Content.ToString());
}
}
}
}
When running this with a breakpoint at the line response.EnsureSuccessStatusCode(), I always get 502 response. However, if running this in Postman or in curl, I got back a valid result.
Example in curl:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "addr=1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P" "https://api.omniexplorer.info/v1/transaction/address"
Many thanks for helping a newbie out!
The request uses application/x-www-form-urlencoded so instead of StringContent use FormUrlEncodedContent:
var content = new FormUrlEncodedContent(req);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(uriBuilder.Uri, content);

HttpRequest.Content.IsMimeMultipartContent() is returning false when it should return true

I need to send an HTTP request as a MultiPartFormData to a REST controller. It was working, but now the check I have on my controller is claiming that the request is not of the correct type, even when I can see in the debugger that the request is on the correct type. For reference:
Here's the console app code that is calling it:
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace QuickUploadTestHarness
{
class Program
{
static void Main(string[] args)
{
using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
{
// Make sure to change API address
client.BaseAddress = new Uri("http://localhost");
// Add first file content
var fileContent1 = new ByteArrayContent(File.ReadAllBytes(#"C:\<filepath>\test.txt"));
fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "testData.txt"
};
//Add Second file content
var fileContent2 = new ByteArrayContent(File.ReadAllBytes(#"C:\<filepath>\test.txt"));
fileContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Sample.txt"
};
content.Add(fileContent1);
content.Add(fileContent2);
// Make a call to Web API
var result = client.PostAsync("/secret/endpoint/relevant/bits/here/", content).Result;
Console.WriteLine(result.StatusCode);
Console.ReadLine();
}
}
}
}
How is it possible that it's being interpreted as not MultiPartFormData? Note the "using MultiPartFormDataContent" for the request
For MultiPartFormDataContent you can try to use the content.Add overload that takes a name and filename argument.
MSDN MultipartFormDataContent.Add Method (HttpContent, String, String)

Categories