PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label content-type. Show all posts
Showing posts with label content-type. Show all posts

Monday, October 31, 2022

[FIXED] How to corectly set the ContentType property in HttpWebRequest (or how to fix the missing Content-Type header)

 October 31, 2022     content-type, contenttype, header, httpwebrequest     No comments   

Issue

I thought I'd share something that took me some time to figure out:

I wrote a simple Post method using HttpWebRequest class. In HttpWebRequest you can't use HttpWebRequest.Headers collection to set your desired headers when there is a dedicated property for it - you must use that dedicated property. ContentType is one of them. So I created my HttpWebRequest like this:

            HttpWebRequest httpWebRequest = (HttpWebRequest)webRequest;
            httpWebRequest.Method = "POST";
            httpWebRequest.KeepAlive = false;
            httpWebRequest.ServicePoint.Expect100Continue = false;
            httpWebRequest.ContentType = "application/json";

somewhere below I set the body of my request like this:

            using (StreamWriter streamWriter = new StreamWriter(streamWebRequest))
            {
                streamWriter.Write(sJson);
            }

and posted the request using:

            WebResponse webResponse = httpWebRequest.GetResponse();

But I kept getting a "400 - Bad Request" error, while the same request worked from Postman. After analyzing the request with Fiddler I found that when I send the request from my app, the Content-Type: application/json header is missing. All the other headers were present, except for Content-Type. I thought I'm setting it wrong, so I googled but didn't find a good answer. After much experimentation I found, that if I move the line:

            httpWebRequest.ContentType = "application/json"

after this block:

            using (StreamWriter streamWriter = new StreamWriter(streamWebRequest))
            {
                streamWriter.Write(sJson);
            }

then the httpWebRequest.ContentType = "application/json" header finally appears in the request. So, for HttpWebRequest make sure you always set your HttpWebRequest's body/content first, before you set the ContentType property.

Hope it helps


Solution

My question above already has the answer, but to mark it as "Answered" I had to add this comment:

Make sure you always set your HttpWebRequest's body/content first, before you set the ContentType property.This way the "Content-Type" header will appear in the request.



Answered By - Alexey Nagoga
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, September 21, 2022

[FIXED] How to change content-type but keep charset on Apache2?

 September 21, 2022     apache, apache2, content-type, header, virtualhost     No comments   

Issue

I have a Apache2 server on Ubuntu for proxy server. I want to change content-type of response to client to another but it will remove charset too.

# /etc/apache2/sites-available/000-default.conf
Header set Content-Type "text/html" "expr=%{resp:Content-Type} =~ m|text/abcdefgh|"  

With this setting, when it see the header which content-type is text/abcdefgh or text/abcdefgh; charset=utf-8 or text/abcdefgh; charset=shift_jis, it will become text/html without charset

  1. Is there any way to change part of content-type by Header set or others?
  2. Where can I find the meaning of this pattern?

Many thanks!


Solution

Did you try to use edit instead of set?

Header edit Content-Type "text/abcdefgh" "text/html"

It should just replace text/abcdefgh with text/html but leave the charset as is



Answered By - Capsule
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing