1

My Environments:

  • Shop RestController : Spring Boot
    • on localhost laptopn ubuntu 18:04
  • ShopService: Angular HttpClient
    • on localhost desktop pc windows 10

Problem:

My get request via HttpClient got the response null. But if I use postman or on browser, I got my expected response body.

Angular Code:

const BASE_URL = 'http://pc01:8081/shopsystem/shops';
const  SHOP_NAME = 'Muster Shop';

@Injectable({
  providedIn: 'root'
})
export class ShopService {

  shop$: Shop;
  address$: Address;
  owner$: Owner;

  //header = new HttpHeaders({ 'Content-Type': 'application/json' });

  constructor(private http: HttpClient) { }

  public getShop(): Observable<Shop>{
    return this.http.get<Shop>(BASE_URL + '/' + SHOP_NAME);
  }

Response-Header:

enter image description here

Response:

enter image description here

Request on postman:

enter image description here

Spring Boot controller:

enter image description here

I have already tried with Observable with any and HttpHeaders with application/json but same response.

Another thing: If my Spring Boot controller returns a List then I got the response body with contents.

Question:

Why I got a null or empty body in my HttpClient request?

How can I solve this?

Thank you

10
  • show your Get method in the spring boot controller Commented Apr 20, 2019 at 11:54
  • Thanks for quick reply. I edited my question with my spring boot controller Commented Apr 20, 2019 at 11:59
  • Have you tried to set it up without a space in the uri? Change muster shop to musterShop and see if you are still getting the null Commented Apr 20, 2019 at 12:01
  • I got 404 like expected from backend. Request URL: pc01:8081/shopsystem/shops/MusterShop Request Method: GET Status Code: 404 Commented Apr 20, 2019 at 12:05
  • 1
    I recon it’s your addon stuffing things up. Turn it off and see what happens. Run the apps on same machine and see what happens. Enable Cors properly in spring boot and see what happens. Looks like you have some debugging to do. Commented Apr 20, 2019 at 15:24

3 Answers 3

1

Your Angular HttpClient encodes the space in the path variable so it comes to the RestController as Muster%20Shop.

Then you try to find a record with that exact name which does not exist.

You have to decode the @PathVariable before you send it to your query as Muster Shop.

Sign up to request clarification or add additional context in comments.

1 Comment

But Spring Boot decode it automatically I can see it on my spring boot log. If my request from HttpClient reach my spring boot its calls the expected service and returns the expected response.
0

add annotation @CrossOrigin to your Controller class

@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
public class UserController {

    private final FileStorageService fileStorageService;
    private final GameService service;


    @GetMapping("{id}")
    public GameDTO getUserbyID(@PathVariable Long id) {
        log.info("get {} at {}", service.getUserbyID(id), Calendar.getInstance().getTime());
        return service.getUserbyID(id);
    }

1 Comment

A more detailed answer would have included a code sample with the recommended fix.
0

It might be resolved but i've been dealing with somenthing similar i think and when it comes to observables in HttpClient you need to add a property in the options param of the http request method. i. e.

enter image description here

It's somenthing that never happened before to me, but since im not the one who made the API cant be sure if there's somenthing in backend that may be related to this issue.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.