2

I need to be able to append an encoded object to a URI to pass it to a Web API endpoint.

In this post, the author is creating an object:

var request = new Object();
request.SearchWindowStart = start.toISOString();
request.SearchWindowEnd = end.toISOString();
request.ServiceId = "5f3b6e7f-48c0-e511-80d7-d89d67631c44";
request.Direction = '0';
request.NumberOfResults = 10;
request.UserTimeZoneCode = 1;

Then they are appending it to a URL:

var req = new XMLHttpRequest()
req.open("GET", clientUrl + "/api/data/v8.0/Search(AppointmentRequest=@request)?@request=" + JSON.stringify(request) , true);

I actually cannot modify the C sharp code however I have two options. The first option is to add the parameters into the URL I actually cannot modify the c# code however I have two options. The first option is to add the parameters into the URL and the other option would be to add a body to the request with my intended object.

If I know the structure of the object ahead of time how do I include it with my request?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • 1
    You shouldn't be sending data through a URL like that, you should be passing it as the body of a REST request... – maccettura Oct 03 '17 at 21:32
  • Possible duplicate of [Can i use JSON.Stringify in code-behind of an ASP.Net project](https://stackoverflow.com/questions/42343342/can-i-use-json-stringify-in-code-behind-of-an-asp-net-project) – John Wu Oct 03 '17 at 21:35
  • @maccettura example please? – Alex Gordon Oct 03 '17 at 21:56

3 Answers3

2

You can do it in a two ways.

  1. Just add every propery of the object with the value to the url eg. /search?property1=value1&property2=value2 Of course each value should be url encoded.

  2. Serialize the whole object into json and send it via post or get. Look at the https://www.newtonsoft.com/json how to do it. Sending request could be done by simple WebClient class.

Smokerei
  • 69
  • 2
1

Based on the code snippet you need to serialize the object to JSON. You can use Json.Net as already linked in the other answer.

Using OP as an example...

var request = new {
    SearchWindowStart = "some_start_value",
    SearchWindowEnd = "some_end_value",
    ServiceId = "5f3b6e7f-48c0-e511-80d7-d89d67631c44",
    Direction = '0',
    NumberOfResults = 10,
    UserTimeZoneCode = 1
};
//JSON.stringify(request)
var json = JsonConvert.SerializeObject(request);
var url = clientUrl + "/api/data/v8.0/Search(AppointmentRequest=@request)?@request=" + json;

From there you should be able to use the URL as desired.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Interesting you're saying that I can use an anonymous type? – Alex Gordon Oct 04 '17 at 02:41
  • Doesn't really matter as you have the object just to serialize it to JSON. If you already have your own types you can serialize those the same way. – Nkosi Oct 04 '17 at 02:42
  • I have only two options. I can either change the url and add parameters to it or I can add a body to the request. I actually cannot modify the C sharp code. – Alex Gordon Oct 04 '17 at 02:45
  • You can only add a body to POST or PUT request. All other request types ignore the body. – Nkosi Oct 04 '17 at 02:46
0
let encodedObj = encodeURIComponent(JSON.stringify(yourObject))

Then you could just.

req.open("GET", clientUrl +  
 "/api/data/v8.0/Search(AppointmentRequest=@request)?@request=" + encodedObj, true);

Although according to: https://javascript.info/url
The encode functions are based on the obsolete version RFC2396, and advises you to use these classes instead:
URL and URLSearchParams to build a URL.

Tally
  • 11
  • 4