0

I've a problem as I need to send some json to a url. When I send all my json and token to the page.

Then there will be no content JSON value into the system.

I have checked up on whether there is some content and it is there, but it sends just do not like json values.

string apiKeyToken = model.reepaytoken; // TOKEN HERE.

string URLLink = APIClassPay.HelperPay.CreateCustomerURL;//URL to send it json to.

WebClient client = new WebClient();
            //JSON coming here!
var JSONCustomer = APIClassPay.HelperPay.CreateCustomer(model.Brugernavn, model.Adresse, model.Byen, model.Postnr.ToString(), model.Mobil.ToString(), model.Fornavn, model.Efternavn);
client.Headers.Add("text/json", JSONCustomer);
client.Headers.Set("X-Auth-Token", apiKeyToken);
string reply = client.DownloadString(URLLink);

When I blow my json looks like this.

[HttpPost]
public ActionResult information(BuyMedlemskabViewModel model)
{
    DataLinqDB db = new DataLinqDB();
    var Pric = db.PriceValues.FirstOrDefault(i => i.id == model.HiddenIdMedlemskab);
    if (Pric != null)
    {
        string _OrderValue = DateTime.Now.Year + Helper.Settings.PlanValue();
        Session[HelperTextClass.HelperText.SessionName.OrderId] = _OrderValue;

        Session[HelperTextClass.HelperText.SessionName.FakturaId] = model.HiddenIdMedlemskab;

        Session[HelperTextClass.HelperText.SessionName.fornavn] = model.Fornavn;
        Session[HelperTextClass.HelperText.SessionName.efternavn] = model.Efternavn;
        Session[HelperTextClass.HelperText.SessionName.Adresse] = model.Adresse;
        Session[HelperTextClass.HelperText.SessionName.Post] = model.Postnr;
        Session[HelperTextClass.HelperText.SessionName.Byen] = model.Byen;
        Session[HelperTextClass.HelperText.SessionName.Mobil] = model.Mobil;


        string apiKeyToken = model.reepaytoken;.

        string URLLink = APIClassPay.HelperPay.CreateCustomerURL;//URL to send it json to.

        WebClient client = new WebClient();
        //JSON coming here!
        var JSONCustomer = APIClassPay.HelperPay.CreateCustomer(model.Brugernavn, model.Adresse, model.Byen, model.Postnr.ToString(), model.Mobil.ToString(), model.Fornavn, model.Efternavn);
        client.Headers.Add("text/json", JSONCustomer);
        client.Headers.Set("X-Auth-Token", apiKeyToken);
        string reply = client.DownloadString(URLLink);


    }
    return RedirectToAction("information");
}

EDIT - Update (ERROR HERE):

enter image description here

ReePay API reference: https://docs.reepay.com/api/

Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
J. Petersen
  • 101
  • 1
  • 9

1 Answers1

1

I think there are a few things, you'll have to fix:

First of all you're obviously trying to create a ressource (usually a POST or PUT, speaking in REST-words but you're using WebClient's DownloadString-method which performs a GET. So I think you should probably use a POST or PUT instead but which one to chose exactly depends on the web service you're contacting.

Then you seem to have mistaken the Content-Type-header and tried to pack the payload in there. The payload - your customer JSON - will have to be put into the request's body.

Based on your previous questions I assume the service you're trying to contact is either PayPal or QuickPay. To further help you with this question, it'd be helpful if you could specify which one you use.

If it's QuickPay, please notice that there's an official .NET client which you could use instead of using WebClient on you own.

But anyway for making HTTP requests I'd suggest you to use HttpClient in favor of WebClient. You'd generally do it in a way like this:

using (var httpClient = new HttpClient())
{
    var request = new HttpRequestMessage(HttpMethod.Post, 
         APIClassPay.HelperPay.CreateCustomerURL); 
    request.Headers.Add("X-Auth-Token", apiKeyToken); 
    request.Headers.Add("Content-Type", "application/json");
    request.Content = new StringContent(JSONCustomer);

    var response = await httpClient.SendAsync(request);
}

EDIT:

As you clarified in a comment, the service you're using is Reepay. If you take a look at the documentation of the create customer method, you can see, that the necessary HTTP method is POST. So the code snippet above should generally fit.

Regarding the compilation error you faced, I updated the code-snipped above. There was a mistake in the variable names I chose. Please note, that you dropped the keyword await as I can see from your screenshot. Please re-enter it. If the compiler complains about it, it's very likely that the .NET framework version of your project is less than 4.5 which is necessary to use async/await.

So you should update your project's .NET framework version at best to version 4.6.1 as Microsoft recently announced that support for 4.5 and others is discontinued. Have a look here on how to do that.

Community
  • 1
  • 1
Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
  • Thank you for your time. Sorry I answer a little late. But I do not Quickpay or paypal. The two solutions can not cope with my requirements of the region for membership on my website. I am aware that they have something on Github, but they do not offer it that I can use the membership on my website. I had just post and google me forward at all possible ways to find a solution that I think there seemed straightforward. Ang your code then gives the error HTTPClient and said users I do not await. Unfortunately. – J. Petersen May 15 '16 at 01:16
  • Well, then can you tell which service you are trying to use instead? Is there a public api reference? What's the error you're getting with HttpClient? The keyword await is unknown? You can fix that by updating the .NET framework version of your project. – Jan Köhler May 15 '16 at 07:11
  • i can see its, Thanks for u answer. I have .Net Framework 4.6. – J. Petersen May 15 '16 at 13:10
  • Are you able to compile now or do you face any other errors? – Jan Köhler May 15 '16 at 13:22
  • Nop, after i delete await it seems that it works. and thank you for your help !. Hope you will give me a +1 for the issue. – J. Petersen May 16 '16 at 01:57