2

I need to pass below object as a parameter for a post method using web client method in C#.

     {
    "company":
    {
        "id": "e63dfcab345260b2591f585126ede56627db4ef2"
    },
    "requestor":
    {
        "id": "",
        "email": "[email protected] ",
        "firstName": "Test",
        "lastName": "Requestor",
        "role": "employerAdmin",
        "phone": "(415)1112222",
        "title": "HR Manager"
    }
}

I converted like this

company[id]=e63dfcab345260b2591f585126ede56627db4ef2&requestor[id]=&requestor[email][email protected]+&requestor[firstName]=Test&requestor[lastName]=Requestor&requestor[role]=employerAdmin&requestor[phone]=(415)1112222&requestor[title]=HR+Manager

But i am getting invalid parameter error. Please help me. Thanks in advance.

My entire code is below:

 using (var Requestor = new System.Net.WebClient())
            {

                string url = "https://stormaas-pre.inflection.com:8443/v1/Requestor?";
                string parameters = "company[id]='757563a3-67df-4a6e-9ef9-d89d57d41e0d'&requestor[id]=''&requestor[email]='[email protected]'&requestor[firstName]='Test'&requestor[lastName]='Requestor'&requestor[role]='employerAdmin'&requestor[phone]='(415)1112222'&requestor[title]='HRManager'";
                Requestor.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                Requestor.Credentials = new System.Net.NetworkCredential("xyz:xyz!", "");
                Requestor.Encoding = System.Text.Encoding.UTF8;
                Requestor.Headers[HttpRequestHeader.ContentType] = "application/json";
                Requestor.Headers[HttpRequestHeader.Accept] = "text/xml";
                string res = Requestor.UploadString(url, "POST", parameters);
            }
3
  • Could you include more information such as the post target and how its being posted Commented Mar 5, 2015 at 9:25
  • Which version of .net framework are you using? Commented Mar 5, 2015 at 9:43
  • Using 4.5 and MVC version 4. Commented Mar 5, 2015 at 9:51

2 Answers 2

5

You can create POCO classes for the Requestor, Company like follows and use JSON.net to do the conversion for you.

public class Company
{
    public string Id { get; set; }
    // Other properties
}

public class Requestor
{
    public string Id { get; set; }

    public string Email { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }
    // Other properties
}

public class Container
{
    public Company Company { get; set; }

    public Requestor Requestor { get; set; }
}

var requestor = new Container();
requestor.Company = new Company { Id = "sampleid" };
requestor.Requestor = new Requestor
{
    FirstName = "test",
    LastName = "testname"
};

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var data = JsonConvert.SerializeObject(requestor, settings);

WebClient client = new WebClient();
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
// Code for the credentials etc
client.UploadString(@"your url", data);

Hope this helps. For this to work, you need to have a reference to JSON.net. Since you are using .net 4.5 with Web API, you should be having the reference already.

Sign up to request clarification or add additional context in comments.

Comments

0

You get invalid parameter error because square bracket are not allowed in a url. If you want use characters that are not include in RFC 1738 you must use HttpUtility.UrlEncode("String")(msdn reference)

2 Comments

Ok! How to convert these two sub object into query string.
Getting same error from API. Please see this link goodhire.com/api-documentation#http

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.