79 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
		
		
			
		
	
	
			79 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
|   | --- | |||
|  | id: 587d7fb2367417b2b2512bf8 | |||
|  | title: Get Data from POST Requests | |||
|  | challengeType: 2 | |||
|  | forumTopicId: 301511 | |||
|  | dashedName: get-data-from-post-requests | |||
|  | --- | |||
|  | 
 | |||
|  | # --description--
 | |||
|  | 
 | |||
|  | Mount a POST handler at the path `/name`. It’s the same path as before. We have prepared a form in the html frontpage. It will submit the same data of exercise 10 (Query string). If the body-parser is configured correctly, you should find the parameters in the object `req.body`. Have a look at the usual library example: | |||
|  | 
 | |||
|  | <blockquote>route: POST '/library'<br>urlencoded_body: userId=546&bookId=6754 <br>req.body: {userId: '546', bookId: '6754'}</blockquote> | |||
|  | 
 | |||
|  | Respond with the same JSON object as before: `{name: 'firstname lastname'}`. Test if your endpoint works using the html form we provided in the app frontpage. | |||
|  | 
 | |||
|  | Tip: There are several other http methods other than GET and POST. And by convention there is a correspondence between the http verb, and the operation you are going to execute on the server. The conventional mapping is: | |||
|  | 
 | |||
|  | POST (sometimes PUT) - Create a new resource using the information sent with the request, | |||
|  | 
 | |||
|  | GET - Read an existing resource without modifying it, | |||
|  | 
 | |||
|  | PUT or PATCH (sometimes POST) - Update a resource using the data sent, | |||
|  | 
 | |||
|  | DELETE => Delete a resource. | |||
|  | 
 | |||
|  | There are also a couple of other methods which are used to negotiate a connection with the server. Except from GET, all the other methods listed above can have a payload (i.e. the data into the request body). The body-parser middleware works with these methods as well. | |||
|  | 
 | |||
|  | # --hints--
 | |||
|  | 
 | |||
|  | Test 1 : Your API endpoint should respond with the correct name | |||
|  | 
 | |||
|  | ```js | |||
|  | (getUserInput) => | |||
|  |   $.post(getUserInput('url') + '/name', { first: 'Mick', last: 'Jagger' }).then( | |||
|  |     (data) => { | |||
|  |       assert.equal( | |||
|  |         data.name, | |||
|  |         'Mick Jagger', | |||
|  |         'Test 1: "POST /name" route does not behave as expected' | |||
|  |       ); | |||
|  |     }, | |||
|  |     (xhr) => { | |||
|  |       throw new Error(xhr.responseText); | |||
|  |     } | |||
|  |   ); | |||
|  | ``` | |||
|  | 
 | |||
|  | Test 2 : Your API endpoint should respond with the correct name | |||
|  | 
 | |||
|  | ```js | |||
|  | (getUserInput) => | |||
|  |   $.post(getUserInput('url') + '/name', { | |||
|  |     first: 'Keith', | |||
|  |     last: 'Richards' | |||
|  |   }).then( | |||
|  |     (data) => { | |||
|  |       assert.equal( | |||
|  |         data.name, | |||
|  |         'Keith Richards', | |||
|  |         'Test 2: "POST /name" route does not behave as expected' | |||
|  |       ); | |||
|  |     }, | |||
|  |     (xhr) => { | |||
|  |       throw new Error(xhr.responseText); | |||
|  |     } | |||
|  |   ); | |||
|  | ``` | |||
|  | 
 | |||
|  | # --solutions--
 | |||
|  | 
 | |||
|  | ```js | |||
|  | /** | |||
|  |   Backend challenges don't need solutions,  | |||
|  |   because they would need to be tested against a full working project.  | |||
|  |   Please check our contributing guidelines to learn more. | |||
|  | */ | |||
|  | ``` |