1

I am getting problems making a http POST. The API I am calling is asking for the content-length. But I am unsure how to do this.

Here is the code I have:

public static string publishClip(string instance_url, string sessionId, string clipId)
{
    int trev = System.Text.ASCIIEncoding.Unicode.GetByteCount(instance_url + "/services/apexrest/DesktopClient/PublishClip/" + clipId);

    WebRequest wrGETURL;
    wrGETURL = WebRequest
        .Create(instance_url
                + "/services/apexrest/DesktopClient/PublishClip/"
                + clipId);
    wrGETURL.Method = "POST";
    wrGETURL.ContentType = "application/json";
    wrGETURL.ContentLength = trev;
    wrGETURL.Headers.Add("Authorization", "Bearer " + sessionId);
    Stream objStreamclipId = wrGETURL.GetResponse().GetResponseStream();
    StreamReader objReader = new StreamReader(objStreamclipId);

    return "trev";
}

Can anyone help me out please?

This is the error I am getting:

{"You must provide a request body if you set ContentLength>0 or SendChunked==true. Do this by calling [Begin]GetRequestStream before [Begin]GetResponse."}

Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89
  • Please be more specific. It can be clearly seen in your code, that you've tried setting the `ContentLength` property. So, what's the problem? Didn't it work? What's wrong with the example here: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contentlength.aspx ? – BartoszKP Oct 02 '13 at 12:10
  • In your case the content length is 0, since your request doesn't have a body... – Thomas Levesque Oct 02 '13 at 12:17
  • Did you set the `ContentLength = 0`? – Christian Phillips Oct 02 '13 at 12:26
  • @BartoszKP apologies, I have now added the error I am getting. – Trevor Daniel Oct 02 '13 at 12:28
  • @christiandev when I set the length to 0 I get "{"The remote server returned an error: (400) Bad Request."}" – Trevor Daniel Oct 02 '13 at 12:30
  • Regarding your comment under one of the answers: in your case content should be the number of bytes in your URL (what you're doing now seems correct), but then you should also add the URL to the post body, as in this example: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contentlength.aspx (I've posted it earlier). Just put your URL for `postData`. – BartoszKP Oct 02 '13 at 12:34
  • @BartoszKP thanks. I have read that article but I am still unclear on how to put that code into mine :( sorry. can you help? – Trevor Daniel Oct 02 '13 at 13:08
  • @TrevorDaniel Look at christiandev's answer - use it, and put your URL into "postData" variable. Probably leave the encoding part the way you're doing it now. – BartoszKP Oct 02 '13 at 13:19

2 Answers2

2
var postData = ?;
byte[] bytes = encoding.GetBytes(postData);
wrGETURL.ContentLength = bytes.Length;

Is your postData Trev?

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
0

You will need to get the Bytes from your StreamReader objReader. To do so, see here: How to convert an Stream into a byte[] in C#? and count them.

Community
  • 1
  • 1
alpham8
  • 1,314
  • 2
  • 14
  • 32