0

I have the following REST API (json object) for search and I should build a POST request in the URL, what what be the url parameters for the following REST API? Just because it's nested I don't know how to build the url where to include question mark or equals signs.

http://localhost:63020/api/search....?

 {
    "offset": 0,
    "batchsize": 10,
    "search": {
          "scope": [2,3,32],
          "type": "basic",
          "text": {
               "value": "test*",
               "fields": [
                     "subject", "body"
                ]
          },
           "age": {
                "unit": "d",
                "amount": 365
         },
         "hasattachments": false,
        },
      "facet": "messagehits",
  }
The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
AlexFF1
  • 1,233
  • 4
  • 22
  • 43
  • First of all, your code is not a rest api, which you seem to call it. It is a Javscript object, or JSON annotation. How you build the URL all depends on the specific rest api, and how it expects you to write your URLs. The link you have provided for us, is localhost, which means it only works on your own computer. There is not much anyone can do to help you, until you improve your question... – jumps4fun Jan 03 '18 at 09:27
  • As @KjetilNordin said, what you posted is a Javascript Object notation. Is a nested search what you're looking for? Assuming you have a url that fetches this object, then you have two options, one is to modify the server to accept certain parameters and filter the results before responding, or filter the response on the client side. In general when it comes to URL common usage, `/` is used for nested properties and `?` is used for providing extra parameters. – fingeron Jan 03 '18 at 09:31
  • Thank you for suggestions, I am really looking what is the common format in the url for the following json object it should include the following parameters in the url to receive a response http://localhost:63020/api?offset=5&batchsize=15... how to include the nested objects in the url, specifically search is an object which have other object so how to format them in the url? – AlexFF1 Jan 03 '18 at 09:32
  • URL parsers do this: `localhost:63020/api?offset=5&batchsize=15` is being interpreted and forwarded to the `localhost:63020/api` function in your server, which is then being given these parameters `offset=5&batchsize=15`. Due to this syntax **it is impossible** to do something like `localhost:63020/api?offset=5&batchsize=15/search?type=basic` – fingeron Jan 03 '18 at 09:35
  • voted to close. There is no way this can be answered without a full explanation / tutorial on how to implement a rest api – 2pha Jan 03 '18 at 09:36
  • Ok and what would be the rest url query from the above object? – AlexFF1 Jan 03 '18 at 09:36
  • similar question https://stackoverflow.com/questions/15872658/standardized-way-to-serialize-json-to-query-string – AlexFF1 Jan 03 '18 at 09:41
  • Hey Oleksandr, is this REST API something that you created, and own, or is it something that someone else has created? – jumps4fun Jan 03 '18 at 09:56
  • Not sure that a POST request is correct either. I have never worked with a REST API using POST yet. Admittedly, I do not know if it is impossible, but it is not common. – jumps4fun Jan 03 '18 at 10:00
  • 1
    Some one else created and did not provide me with the actual format, I am just reading that in Angular 4 you can send the params as the object itself, still looking for a proper solution – AlexFF1 Jan 03 '18 at 10:01

2 Answers2

0

since you want to use Post request no need to modify your URL(https://www.w3schools.com/tags/ref_httpmethods.asp) , just pass it as document body, and process it server side.

mahendra
  • 367
  • 5
  • 18
0

Ok so figured it out in Anagular 4 I am using HTTP client module, what I did I just passed the whole object as a parameter and I got a response back, simple as that, see the following code:

sampleSearch(){
    this.http.post('/api/search/', {
        "offset": 0,
        "batchsize": 10,
        "search": {
               "scope": [2,3,32],
               "type": "basic",
               "text": {
                      "value": "test*",
                      "fields": [
                             "subject", "body"
                      ]
               },
               "age": {
                      "unit": "d",
                      "amount": 365
               },
               "hasattachments": false,
        },
        "facet": "messagehits",
    }
     ).map(data => data).subscribe(
        res => {
          console.log(res);
        },
        err => {
          console.log("Error occured");
        }
    );

}
AlexFF1
  • 1,233
  • 4
  • 22
  • 43