Adding JSON marshalling
	
		
			
	
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
					Details
				
			
		
	
				
					
				
			
				
	
				continuous-integration/drone/push Build is passing
				
					Details
				
			
		
	
							parent
							
								
									9fa3b1d6c6
								
							
						
					
					
						commit
						0ef484a7d4
					
				| @ -0,0 +1,33 @@ | |||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"net/http" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func (app *application) writeJSON(w http.ResponseWriter, status int, data any, headers http.Header) error { | ||||||
|  | 
 | ||||||
|  | 	js, err := json.Marshal(data) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	js = append(js, '\n') | ||||||
|  | 
 | ||||||
|  | 	// At this point, we know that we won't encounter any more errors before writing the
 | ||||||
|  | 	// response, so it's safe to add any headers that we want to include. We loop
 | ||||||
|  | 	// through the header map and add each header to the http.ResponseWriter header map.
 | ||||||
|  | 	// Note that it's OK if the provided header map is nil. Go doesn't throw an error
 | ||||||
|  | 	// if you try to range over (or generally, read from) a nil map.
 | ||||||
|  | 	for key, value := range headers { | ||||||
|  | 		w.Header()[key] = value | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Add the "Content-Type: application/json" header, then write the status code and
 | ||||||
|  | 	// JSON response.
 | ||||||
|  | 	w.Header().Set("Content-Type", "application/json") | ||||||
|  | 	w.WriteHeader(status) | ||||||
|  | 	w.Write(js) | ||||||
|  | 
 | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in New Issue