Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The 'Accept' header must be modified using the appropriate property or method. Parameter name: name

Getting error message - The 'Accept' header must be modified using the appropriate property or method. i'm getting the response in the post man but not getting in the MVC controller i have one basic authentication api with below headers.while using adding accept headers in the request getting the error

Content-Type:  application/vnd.onem2m-res+json;ty=4;
Accept: application/vnd.onem2m-res+json;
Cache-Control: no-cache
X-M2M-RI: 9900001
Authorization: Basic QzdBQUNFO
X-M2M-Origin: C7AACE9-25

code

WebRequest req = WebRequest.Create(@"url");
                req.Method = "GET";
                req.Headers["Authorization"] = "Basic " + "QzdBQUNFO";
                req.ContentType = "application/vnd.onem2m-res+json";
                req.Headers.Add("Accept", "application/vnd.onem2m-res+json;");
                req.Headers["Cache-Control"] = "no-cache";
                req.Headers["X-M2M-RI"] = "9900001";
                req.Headers["X-M2M-Origin"] = "C7AACE9-25";
                HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                var encoding = resp.CharacterSet == "" ? Encoding.UTF8 : Encoding.GetEncoding(resp.CharacterSet);
like image 854
krishna mohan Avatar asked May 19 '18 08:05

krishna mohan


People also ask

What is the use of Accept header in REST API?

The Accept request HTTP header indicates which content types, expressed as MIME types, the client is able to understand. The server uses content negotiation to select one of the proposals and informs the client of the choice with the Content-Type response header.

Is Accept header necessary?

Servers may ignore the Accept header. If you're not returning anything in your response, it's kind of meaningless. It's up to you to decide whether you want to reject requests with Accept headers or not.


1 Answers

WebRequest req = WebRequest.Create(@"url");
            req.Method = "GET";
            req.Headers["Authorization"] = "Basic " + "QzdBQUNFO";
            req.ContentType = "application/vnd.onem2m-res+json";
            req.Accept = "application/vnd.onem2m-res+json";
            req.Headers["Cache-Control"] = "no-cache";
            req.Headers["X-M2M-RI"] = "9900001";
            req.Headers["X-M2M-Origin"] = "C7AACE9-25";
            HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
            var encoding = resp.CharacterSet == "" ? Encoding.UTF8 : Encoding.GetEncoding(resp.CharacterSet);

microsoft docs

like image 87
Coen Avatar answered Sep 23 '22 15:09

Coen