Как правильно использовать почтовый запрос в Angular, отправляя строку на сервер json - PullRequest
0 голосов
/ 06 октября 2019

Я работаю с json-server, и я получил что-то подобное, в json объекте я хочу иметь свойства с именем dates, которые являются объектами с числовыми свойствами. Я хочу добавить в этот json объект по post имени даты запроса с его свойствами;Вот json-файл:

{
  "02.10.2019": {
    "301": {
      "status": "free",
      "price": 20000,
      "checkIn": "28.09.2019",
      "checkOut": "02.10.2019",
      "overallPrice": 80000
    },
    "302": {
      "status": "engaged",
      "price": 20000,
      "checkIn": "28.09.2019",
      "checkOut": "02.10.2019",
      "overallPrice": 80000
    },
    "303": {
      "status": "dirty",
      "price": 20000,
      "checkIn": "28.09.2019",
      "checkOut": "02.10.2019",
      "overallPrice": 80000
    },
    "304": {
      "status": "free",
      "price": 20000,
      "checkIn": "28.09.2019",
      "checkOut": "02.10.2019",
      "overallPrice": 80000
    }
  }
}

Я просто хочу добавить второе свойство с именем даты, и я хочу сделать это динамически с помощью функции, которая получает строковую дату, а с помощью httpClient добавляет кjson-server.

import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs';
import {PutNewDateService} from './put-new-date.service';

@Injectable({
  providedIn: 'root'
})
export class FirstGetService {
  constructor(private http: HttpClient, private dateNew: PutNewDateService) { }
  url1 = 'http://localhost:3000/';
  private customizing(url: string, date: string): string {
    return url + date;
  }
  getDate(date: string): Observable<{}> {
    return this.http.get(this.customizing(this.url1, date));
  }
  setDate(date: string): Observable<any> {
    return this.setGoOn(date);
    // return this.http.post(this.customizing(this.url1, date), this.dateNew.getEmptyRooms());
  }
  private setGoOn(date: string) {
    return this.http.post(this.customizing(this.url1, ''), date);
  }
  setSomething(data: string): Observable<any> {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json'
      })
    };
    return this.http.post('http://localhost/', data, httpOptions);
  }
}

1 Ответ

0 голосов
/ 07 октября 2019

Просто создайте правильную полезную нагрузку - добавьте столько свойств, сколько хотите - и отправьте ее.

  setSomething(data: string): Observable<any> {
    const payload={
        somefield: somevalue,
        anotherfield:anothervalue,
        actualData:data,
    }
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json'
      })
    };
    return this.http.post('http://localhost/', payload, httpOptions);
  }

Теперь сервер должен принять сообщение, закодированное в json, чтобы правильно обработать такое сообщение, что буквально payload содержимое будет отправлено как тело запроса.

...